Joined January 2019
·

Ben Saxon

Posted to Be careful with setTimeout in loops over 1 year ago

for (i = 1; i <= 5; ++i) {
setDelay(i);
}

function setDelay(i) {
setTimeout(function(){
console.log(i);
}, 1000 * i);
}

This frustrated me, in my case I needed the '* i' as shown above. Hope this helps anyone else suffering the same problem!

Achievements
1 Karma
0 Total ProTip Views