I'm trying to print out a number every 5 seconds:
for(i=0; i<10; i++) {
setTimeout(function() {
console.log(i)
}, 5000 * i);
}
But instead I print 10
every 5 seconds because the loop finished and the global variable i
is ten. Is there a way to avoid this happening?