스코프와 비동기적 실행
function countdown() {
let i; // i를 for문 밖에서 선언
console.log("Countdown");
for(i = 5; i >= 0; i--) {
setTimeout(() => {
console.log(i === 0 ? "GO!" : i);
}, (5-i)*1000);
}
countdown();
}5
4
3
2
1
GO!-1
-1
-1
-1
-1
-1왜 그럴까
참고 자료
Last updated