-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[렉시컬 스코프, 클로저 개념] setInterval 내 람다의 left 변수는 왜 그대로일까? (재현님 공유) #21
Comments
reproduction video 2024-03-16.12.30.27.mov |
This comment was marked as off-topic.
This comment was marked as off-topic.
아래 영상과 같이 관련해서 위와같은 동작이 일어난 이유를 나름 생각해본 내용은 아래와 같습니다.
정확한지는 모르겠지만! 일단 제가 유추하기로는 이렇습니다! useEffect(() => {
if (isStart) {
setInterval(() => {
const left = computeTime(time);
setTime(left);
console.log(`left time seconds: ${time}`);
}, 1000);
}
}, [isStart, time]); working video2024-03-16.1.21.25.mov |
클로저, 함수를 생성한 시점에 소코프 체인에서 들고온 변수를 저장하고 있음
referenceshttps://ziszini.tistory.com/64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
문제의 코드.. useEffect > setInterval 람다 > left 변수가 왜 그대로인가?
요약: 결국 아래 코드의 문제는 setInterval 람다 생성 시 그때 당시 스코프 체인에 따라 그때 시점의 App 스코프 렉시컬 환경을 저장하고 있고, 그때 자유 변수 time=1500 을 스냅샷찍음 > setInterval 내부에서 람다 레퍼런스를 들고 있어 해당 렉시컬 환경이 사라지지 않고, 따라서 매번 호출할때마다 time 1500을 들고오게됨
The text was updated successfully, but these errors were encountered: