Forum Discussion
Javascript countdown timer stops when tab minimized
Unfortunately, I wasn’t able to replicate the issue using your code. I tested it in Chrome, Edge, and Firefox, and everything seemed to work as expected.
That said, it’s still possible for the issue to occur. Modern browsers often throttle timers in background tabs to conserve CPU and battery. When a tab becomes inactive, setInterval(timer, 1000) isn’t guaranteed to run every second — it may slow down or become inconsistent.
If the issue keeps happening, a more reliable approach would be to calculate the remaining time based on the actual system clock, rather than relying on setInterval alone. This helps ensure the countdown stays accurate even when the tab is minimized or inactive.
That said, I can’t guarantee this approach will fully avoid the issue if it’s already happening with your original setup. But it usually helps improve reliability in most cases.
Example:
function zeros(i) {
return (i < 10 ? "0" : "") + i;
}
var duration = getVar("Countdown_Duration");
var fin = "Done";
var endTime = new Date().getTime() + duration * 1000;
var counter = setInterval(function () {
var now = new Date().getTime();
var remaining = Math.round((endTime - now) / 1000);
if (remaining <= 0) {
clearInterval(counter);
setVar("Countdown_Display", "00:00");
setVar("Countdown_Finished", fin);
return;
}
var minutes = Math.floor(remaining / 60);
var seconds = remaining % 60;
var totalTime = zeros(minutes) + ":" + zeros(seconds);
setVar("Countdown_Display", totalTime);
}, 1000);
Related Content
- 6 months ago