function initTimer(timeLeft) {
var Me = this,
TotalSeconds = 35,
Seconds = Math.floor(timeLeft);
var x = window.setInterval(function() {
var timer = Seconds;
if(timer === -1) { clearInterval(x); return; }
$('#div').html('00:' + (timer < 10 ? '0' + timer : timer));
Seconds--;
},1000);
}
I have this code. Everything works fine, when this tab is active in browser, but when I change tab and return in tab later it has problems. To be more precise, it Incorrectly displays the time.
I'd also tried setTimeout, but problem was the same.
One idea, which I have is: HTML5 Web Workers...
But here is another problem... browsers support.
can someone help to solve this problem? How can I write setInterval, which works properly,even when tab is not active
setIntervalcallbacks when the tabs are not active. You need to take this into account when designing your code.Date.now()setIntervalonly guarantees a minimum time until your code is called. If you're doing time based calculations, you're better off keeping hold of the "starting time" and doing further calculations based off that, rather than incrementing/decrementing a variable.