Reputation: 634
I'm trying to create a simple interval but it's working slower than expected. I should see message every 100ms but instead I see it every 1 second or so. I just don't see what's wrong with the following code:
var readyWaitElapsed = 0;
var readyWait = window.setInterval(function(){
readyWaitElapsed += 100;
console.log("Elapsed value", readyWaitElapsed);
if (readyWaitElapsed >= 1000){
clearInterval(readyWait);
console.log("Timeout !");
}
}, 100);
When I paste it to the Chrome console I only see "Elapsed value"-message every 1 seconds or so and the clearInterval() "timeout" takes at least 10 seconds to finish.
Does anyone have any idea?
Upvotes: 1
Views: 106
Reputation: 634
Turns out I was running the code on an inactive tab while the console was active. Apparently when a tab is not active (not in focus) at least Chrome slows down interval and timeout execution to save resources.
This slowdown doesn't seem to be very accurately fixed to any value so it can't be relied either.
Upvotes: 1