Skip to main content
05:00

A countdown that counts ticks runs late; one that watches the clock does not

Browsers deliberately slow down timers in tabs you are not looking at. A countdown built by subtracting a fixed amount on every tick loses exactly as much time as the browser withheld, so it finishes late — sometimes by minutes. This one stores the moment it should end and compares against the system clock on every update, which makes throttling irrelevant.

How it works

  • Turns hours, minutes and seconds into a single duration and counts it down.
  • Records a target end time when you press start, then derives the display from the clock.
  • Pauses and resumes on the remaining time, and locks the inputs while it is running.
duration = hours × 3,600,000 + minutes × 60,000 + seconds × 1,000  (milliseconds)
deadline = now + duration
remaining = deadline − now, re-read on every update
finished when remaining ≤ 0

Worked example

A five-minute timer, started and then left in a background tab.

  1. duration = 5 × 60,000 = 300,000 ms
  2. deadline is fixed the instant you press start
  3. a tick-counting timer subtracts a fixed step each update, so missed updates are lost time
  4. this one recomputes deadline − now, so missed updates cost nothing
  5. when you return to the tab the reading is already correct

The countdown is accurate whether you watch it or not. Any timer that accumulates its own decrements instead of consulting the clock will drift under exactly the conditions you are most likely to use it in.

Reading the result

  • The timer lives in the page. Closing the tab destroys it, and no alarm will reach you afterwards — for anything you genuinely must not miss, use a device alarm that survives the browser.
  • Pausing keeps the remaining time rather than the original duration, so resuming continues from where you stopped. Reset returns to whatever the inputs currently say, which is why the inputs are locked while it runs.
  • Displayed seconds are rounded up, so a timer showing 1:00 has between fifty-nine and sixty seconds left. Counting down to zero this way means the display reaches 0:00 exactly when the time is genuinely gone rather than a second early.
  • Background tabs may also delay the moment the finish is noticed. The remaining time will be correct the instant the page runs again, but a browser that has suspended the tab entirely cannot tell you at the precise second it expired.

Common questions

Will the timer keep running if I switch tabs?
Yes, and it will stay accurate. Because the end time is fixed when you start and the display is derived from the system clock, throttling in a background tab changes how often the number is redrawn but never what the number is.
Can I close the tab and still be alerted?
No. A countdown running in a page stops existing when the page does. If missing it would matter, set an alarm on your phone or operating system as well.