Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140190

Javascript for loop with settimeout time change

$
0
0

So I'm trying to increase the time out in a for loop, I followed the answer here setTimeout with Loop in JavaScript but the duration of the setTimeout is just not working in milliseconds when I output the date to check the time difference.

Code:

for (var count = 0; count < 12; count++) {
    (function(num){setTimeout(function() {
        console.log("Count = " + num + " Time: " + 1000 * num + " Stamp: " + new Date());
    }, 1000 * num)})(count);
}

As you can see in the console, it's still firing the console.log every 1000 instead of 1000 * count.

I hope the above makes sense and hopefully someone can explain this behaviour.


Viewing all articles
Browse latest Browse all 140190


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>