setInterval countdown falls behind in Edge when the tab is inactive

Muhammad Zayn 60 Reputation points
2026-10-24T03:40:22.3633333+00:00

Hi all,

I am building a small browser game (can check it at free krillion) where each round lasts 25 seconds. The countdown is a plain setInterval that subtracts 1 from a "remaining" variable every 1000 ms and ends the round when it reaches zero.

In Chrome this stays accurate even if I switch tabs for a while. In Edge on Windows 11, the countdown is several seconds behind when I come back after 30 to 60 seconds on another tab. I am guessing this is the background timer throttling or sleeping tabs behaviour, but I could not find a clear statement of what developers are expected to do about it.

I have rewritten it to calculate the remaining time from Date.now() differences whenever the interval fires, which fixes the drift. Is that the intended approach, or is there something Edge-specific I am missing? For example, should the game pause itself when the page visibility state changes to hidden?

Thanks for any pointers.

Microsoft Edge | Speed and responsiveness | Windows 11
0 comments No comments

Answer accepted by question author

AI answer

2026-10-24T03:41:17.0266667+00:00

Use an absolute round deadline and derive the displayed remainder from the current time; setInterval should only trigger updates, not serve as the game’s clock.

const ROUND_MS = 25_000;
let deadline;
let timerId;

function startRound() {
  deadline = Date.now() + ROUND_MS;
  clearInterval(timerId);

  const update = () => {
    const remainingMs = Math.max(0, deadline - Date.now());
    renderCountdown(Math.ceil(remainingMs / 1000));

    if (remainingMs === 0) {
      clearInterval(timerId);
      endRound();
    }
  };

  update();
  timerId = setInterval(update, 250);
}

If Edge delays several callbacks, the next callback immediately calculates the correct remaining time instead of subtracting one second for every callback that happened to run. This is the intended robust pattern because background JavaScript timers are not guaranteed to run at their requested cadence.

Edge explicitly supports intensive wake-up throttling, under which background timers can be coalesced and run no more than once per minute after the page has been backgrounded for at least five minutes.^1^ Shorter delays can also affect callback timing, so timer cadence should never determine elapsed game time.

Whether to pause while hidden

That is a game-rule decision rather than an Edge requirement:

  • The round continues in real time: Keep the absolute deadline. On return, recompute the state and finish the round immediately if the deadline passed.
  • The round pauses when not visible: Use the Page Visibility API to record when the page became hidden, then extend the deadline by the hidden duration when it becomes visible again.^2^
let hiddenAt = null;

document.addEventListener("visibilitychange", () => {
  if (document.hidden) {
    hiddenAt = Date.now();
    clearInterval(timerId);       // Stop unnecessary updates.
  } else if (hiddenAt !== null) {
    deadline += Date.now() - hiddenAt;
    hiddenAt = null;
    resumeCountdownUpdates();
  }
});

For a competitive or networked game, make the server’s round deadline authoritative; otherwise, changing the client clock or suspending the device can affect a Date.now()-based client-only result.

Sleeping tabs are separate

Sleeping Tabs is distinct from ordinary timer throttling. Edge can suspend an inactive tab after its configured inactivity timeout. The documented default is 900 seconds (15 minutes), although users or administrators can configure values as low as 30 seconds; Energy Saver can also select a shorter timeout.^3^

Therefore, a 30–60 second test does not by itself establish that Sleeping Tabs caused the delay. More importantly, disabling Sleeping Tabs would not make setInterval a reliable clock. The application should reconcile from its deadline whenever execution resumes, regardless of whether the interruption came from throttling, a sleeping tab, device sleep, or main-thread congestion.

For smooth foreground rendering, requestAnimationFrame can update the visible countdown, while the absolute deadline remains the source of truth.^4^


References

  1. Microsoft Edge Browser Policy Documentation ...
  2. Page Visibility API - Web APIs | MDN - MDN Web Docs
  3. Microsoft Edge Browser Policy Documentation SleepingTabsTimeout
  4. Window: requestAnimationFrame () method - Web APIs | MDN
AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.