Countdown timers, and keeping them honest in a background tab
Why a countdown that keeps running when the tab is hidden is built differently from one that does not, how browser alerts actually work, and when a timer is the wrong tool.
Last reviewed · 1,384 words
In short
- Compute the remaining time from a target timestamp on each frame. Never decrement a counter, or the timer runs slow whenever the tab is hidden.
- Browsers block audio that was not started by a user action, so a timer must be started with a click for its alarm to sound.
- Notifications need permission, and the browser must still be running for one to fire.
- A countdown to a wall-clock time must handle daylight saving in the target's zone, not the viewer's.
- For anything that must fire when the device is asleep, use the operating system's own timer rather than a web page.
A countdown timer has the same underlying problem as a stopwatch and one additional one: it has to do something when it reaches zero.
Compute, do not decrement
The wrong implementation subtracts one from a counter every second. It runs slow, because each interval fires late and the lateness accumulates, and it stops almost entirely in a hidden tab where timers are throttled to roughly once per second and eventually once per minute.
The correct implementation records the target time once and computes the remainder on each frame:
target = performance.now() + durationMs
// on each frame:
remaining = target - performance.now()
Switch away for ten minutes and switch back, and the display is immediately correct. Nothing accumulated, so nothing drifted.
This is testable in about thirty seconds and it separates a countdown that can be relied on from one that cannot.
Making it fire
Reaching zero is the easy part. Getting the user's attention is where browsers impose real constraints.
Audio requires a user gesture. Browsers block audio that was not initiated by a click or a tap, to stop pages playing sound unprompted. A timer started by a click can play its alarm; a timer started automatically on page load usually cannot.
The workaround used by well-built timers is to create and unlock the audio context at the moment the user presses start, so the alarm is permitted later. It is a real constraint rather than a bug, and it is why some timers are silent.
Notifications require permission, granted explicitly by the user and revocable. Once granted, a notification can appear even when the tab is not focused — but the browser must still be running. Close it and nothing fires.
The tab title is the most reliable signal that survives everything. Updating it to show the remaining time means a glance at the tab strip is enough, with no permissions and no audio.
A visual flash or colour change works only if the tab is visible, which is the case it is least needed for.
For anything genuinely important, the honest answer is that a web page is the wrong place for it. A phone alarm or an operating system reminder fires when the device is asleep, when the browser is closed, and when the laptop lid is shut. A browser tab does none of those.
Counting down to a date
A countdown to a fixed future moment — an exam, a launch, a deadline — has a different problem: time zones.
The target has a zone. An event at 9 am on 1 October in Delhi is not 9 am on 1 October for a viewer in London, and a countdown showing the same remaining time to both is only correct if it is computed from a single absolute instant.
The right model is to store the target as a UTC timestamp and compute the difference from the viewer's current time. Every viewer then sees a correct countdown to the same moment, whatever their zone.
Daylight saving applies to the target's zone, not the viewer's. An event scheduled in local time in a zone that observes DST shifts in absolute terms when the clocks change, and a countdown computed from a naive local time will be an hour out on one side of the transition.
A long countdown must handle the viewer's clock being wrong. A device with a clock several minutes off will show a countdown several minutes off, and there is no way for the page to know. For anything where the exact moment matters, the server's time is the authority.
Where a countdown works well
Cooking. The canonical use, and one where a phone timer is usually better because it survives the screen locking.
Interval training. Work and rest intervals, ideally with an audible cue so the athlete is not watching a screen. The Pomodoro pattern — 25 minutes of work, 5 of rest — is the same shape.
Timed practice. Exam conditions, presentation rehearsal, a timed writing exercise. A visible countdown changes behaviour in a way a clock does not.
Meetings. A visible timer for each agenda item is an unusually effective intervention, and it works because everyone can see it rather than because it makes a sound.
Auctions and releases, where a shared countdown coordinates attention on a single moment.
Where it works badly
Fake urgency. A countdown that resets on refresh, or that has been showing "offer ends in 2 hours" for six months, is a dark pattern. It is also increasingly ineffective, since people recognise it, and in several jurisdictions it is a consumer protection issue.
Anything the device must wake up for. A browser tab is not an alarm clock.
Very long countdowns. A page counting down to something three months away is a novelty. Nobody leaves a tab open for three months, and the value is in the number rather than the counting.
High-stakes timing. Examinations, competitions and anything auditable need server-authoritative timing, because a client-side countdown can be paused with the developer tools.
Accessibility
A timer that only communicates visually excludes people who cannot see it, and one that only communicates by sound excludes people who cannot hear it.
Announce the remaining time to screen readers at intervals rather than continuously — an aria-live region updated every minute rather than every second, since a per-second announcement is unusable.
Provide a non-audio completion signal. A colour change, a message, and the tab title.
Do not rely on colour alone for the warning state.
Allow pausing. A timer that cannot be paused is hostile in almost every context, and it is essential for anyone who needs to step away.
Respect reduced motion. A flashing completion animation can trigger discomfort, and prefers-reduced-motion is the signal to use a static change instead.
Pomodoro and the timed work patterns
The best-known use of a countdown is the Pomodoro technique: 25 minutes of focused work, a 5-minute break, and a longer break after four cycles.
The specific numbers are arbitrary — Francesco Cirillo used a kitchen timer shaped like a tomato — and the mechanism is not.
A committed interval removes the decision. The hardest part of focused work is starting, and a timer converts "work on this" into "work on this for 25 minutes", which is a much smaller commitment.
The break is the point. Attention degrades without recovery, and a scheduled break prevents the slow drift into unproductive tiredness.
Interruptions get deferred rather than absorbed. The convention is to note an interruption and return to it at the break, which protects the interval.
Variants exist for a reason: 50/10 suits deep work better than 25/5, and people who find the interruption of a 25-minute bell disruptive should use a longer interval rather than abandoning the method.
Timing intervals in training
Interval training is the other major use, and the requirements differ.
Audible cues matter more than a display, because the athlete is not looking at a screen. A distinct sound for the start of work and the start of rest is the minimum; a three-second warning before each transition is better.
Repeats need to be automatic. A timer that requires a tap between intervals is unusable mid-set.
The screen must stay awake. Phones dim and lock, and a web page cannot prevent that reliably — the Screen Wake Lock API exists and support is uneven. This is the strongest practical argument for a dedicated app over a browser tab for training.
Total elapsed time matters alongside the interval, so a session can be compared with the last one.
For anything more structured than work-rest — a Tabata protocol, a ladder, a pyramid — a general countdown is the wrong tool and a programmable interval timer is the right one.
What this tool assumes
- Remaining time is computed from a target timestamp on each frame, so it does not drift and it survives a hidden tab.
- Audio requires the timer to have been started by a click; notifications require permission and a running browser.
- The display updates at the screen refresh rate and pauses when the tab is hidden, while the underlying value stays correct.
- Your device's clock is assumed correct; a countdown to a fixed date is only as accurate as it is.
- Nothing is transmitted. The timer runs entirely in your browser, and it will not fire once that browser is closed.