Improving performance and responsiveness of Edge on Windows 11
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
Improving performance and responsiveness of Edge on Windows 11
AI answer
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.
That is a game-rule decision rather than an Edge requirement:
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 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