11.8. A countdown that stays right in a background tab
Topics
This question is in
11. TimersA 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
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), wheremsLeftis the time until the end, measured withDate.now().onDone(): optional; called once, when the countdown reaches 0.- Returns
stop(), which ends the countdown early.
Requirements
onTick(seconds)is called straight away, during the call.- With timers that fire on time,
onTickgets every value fromseconds - 1down to0, each at the moment it becomes true:9at 1000 ms,0at 10000 ms for a 10-second countdown. Never earlier. onDone()is called once, right afteronTick(0), and then no timer is left.- When timers fire late, each
onTickvalue is computed fromDate.now()at that moment, never from the number of calls so far. Values that passed while the timer was late are skipped:onTicknever repeats a value, never goes up, and never goes below 0. - 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.
- If the timer fires long after the end, the countdown goes straight to
onTick(0)andonDone(), once. stop()ends the countdown: no moreonTick, noonDone, no timer left. It works from insideonTick, and after the end it does nothing.startCountdown(0, …)callsonTick(0)andonDone()at once.- At most one timer is waiting at any time, and two countdowns do not affect each other.
Examples
startCountdown(3, (s) => console.log(s), () => console.log("done"));
// 0 ms: 3
// 1000 ms: 2
// 2000 ms: 1
// 3000 ms: 0, done// 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, doneconst stop = startCountdown(60, (s) => { if (s === 55) stop(); });
// 60, 59, 58, 57, 56, 55, then nothing moreEnvironment
- Plain JavaScript in a browser page; the tests call the global
startCountdownfromcountdown.js. - The tests run on a virtual clock that replaces
setTimeout,setInterval, their clear functions,Date.nowandperformance.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
- What is the one number you can compute at the start and never need to change?
- 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