Skip to curriculum
Jump to the editor

11.8. A countdown that stays right in a background tab

Medium
Topics

This question is in

11. Timers
20–30 min

A sale page shows "Offer ends in 10:00", a quiz shows the seconds left, a sign-in code expires in 60 seconds. The obvious countdown subtracts 1 every time a one-second timer fires. In a foreground tab that drifts a little; in a background tab it falls far behind, because browsers throttle timers in hidden tabs to save battery: a 1-second timer may fire after several seconds, or once a minute. Come back to the tab and the countdown says 52 seconds left when the time is already up.

The fix is to stop counting timer calls. Decide once when the countdown ends, and every time a timer does fire, read the clock and show what is really left.

Write startCountdown(seconds, onTick, onDone) in countdown.js.

Signature

JAVASCRIPT
function startCountdown(seconds, onTick, onDone) // returns stop()
  • seconds: a whole number, 0 or more.
  • onTick(secondsLeft): called with the whole seconds left, Math.ceil(msLeft / 1000), where msLeft is the time until the end, measured with Date.now().
  • onDone(): optional; called once, when the countdown reaches 0.
  • Returns stop(), which ends the countdown early.

Requirements

  1. onTick(seconds) is called straight away, during the call.
  2. With timers that fire on time, onTick gets every value from seconds - 1 down to 0, each at the moment it becomes true: 9 at 1000 ms, 0 at 10000 ms for a 10-second countdown. Never earlier.
  3. onDone() is called once, right after onTick(0), and then no timer is left.
  4. When timers fire late, each onTick value is computed from Date.now() at that moment, never from the number of calls so far. Values that passed while the timer was late are skipped: onTick never repeats a value, never goes up, and never goes below 0.
  5. After a late timer, the next update aims at the next change of the whole second, not a fixed 1000 ms later: if a timer fires at 1300 ms, the next one is set for 2000 ms.
  6. If the timer fires long after the end, the countdown goes straight to onTick(0) and onDone(), once.
  7. stop() ends the countdown: no more onTick, no onDone, no timer left. It works from inside onTick, and after the end it does nothing.
  8. startCountdown(0, …) calls onTick(0) and onDone() at once.
  9. At most one timer is waiting at any time, and two countdowns do not affect each other.

Examples

JAVASCRIPT
startCountdown(3, (s) => console.log(s), () => console.log("done"));
// 0 ms:    3
// 1000 ms: 2
// 2000 ms: 1
// 3000 ms: 0, done
JAVASCRIPT
// In a background tab where every timer waits at least 3.7 seconds:
startCountdown(10, (s) => console.log(s), () => console.log("done"));
// 0 ms:     10
// 3700 ms:  7    (6.3 s left: 9 and 8 were skipped)
// 7400 ms:  3    (2.6 s left)
// 11100 ms: 0, done
JAVASCRIPT
const stop = startCountdown(60, (s) => { if (s === 55) stop(); });
// 60, 59, 58, 57, 56, 55, then nothing more

Environment

  • Plain JavaScript in a browser page; the tests call the global startCountdown from countdown.js.
  • The tests run on a virtual clock that replaces setTimeout, setInterval, their clear functions, Date.now and performance.now. To imitate a background tab, some tests make every timer fire later than asked.
  • Pausing and showing minutes are not part of this problem.

Hints

  1. What is the one number you can compute at the start and never need to change?
  2. If 5.7 seconds are left, the display shows 6. How long until it should show 5?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…