My delayed for loop keeps returning "-1" no matter what i do.
for (var i = 5; i > 0; i--) {
setTimeout(function() {
console.log(i)
}, i * 1000)
}
(i changed my variable to 5)
My delayed for loop keeps returning "-1" no matter what i do.
for (var i = 5; i > 0; i--) {
setTimeout(function() {
console.log(i)
}, i * 1000)
}
(i changed my variable to 5)
The simplest way will be to call a function inside the for
and let that function handle the setTimeout
for (var i = 5; i > 0; i--) {
fnSetTimeout(i);
}
function fnSetTimeout(i) {
setTimeout(function() { console.log(i); }, 1000 * i);
}