Skip to curriculum
Jump to the editor

11.5. Clear every pending timer

Medium
Topics

This question is in

11. Timers
20–30 min

A single-page app leaves a screen, a test finishes, a widget is removed, and some timer somewhere is still waiting to run code against a page that no longer exists. The browser has no "clear every timer" function and no way to list the timers that are waiting. The only way to know them all is to wrap the functions that create them, recording each id as it is handed out and forgetting it once the timer is done.

Write trackTimers() in track-timers.js. It replaces window.setTimeout, window.setInterval, window.clearTimeout and window.clearInterval with versions that behave the same but keep a record of every timer still waiting. It returns controls to clear them all, count them and put the originals back.

Signature

JAVASCRIPT
function trackTimers() // returns { clearAll, pendingCount, restore }
  • clearAll(): cancels every recorded timer that is still waiting, and returns how many it canceled.
  • pendingCount(): how many recorded timers are still waiting.
  • restore(): puts the four original functions back on window, exactly as they were.

Requirements

  1. After trackTimers(), the four window functions are replaced, and they still behave as before: same delays, extra arguments passed to the callback, the same kind of id, and the clear functions still cancel.
  2. Every timeout and interval created after trackTimers() is recorded while it is waiting: pendingCount() counts them.
  3. A timeout that has run is forgotten, even if its callback throws. An interval stays recorded across its runs until it is cleared.
  4. clearTimeout and clearInterval forget the timer they cancel. Either one works on either kind of id, as in a browser. An unknown id is ignored.
  5. clearAll() cancels every recorded timer, timeouts and intervals alike, so none of them ever runs again, and returns how many there were. Called again with nothing waiting, it returns 0.
  6. Timers created before trackTimers() are not recorded and clearAll() leaves them alone.
  7. Timers created inside a timer callback are recorded too.
  8. restore() puts back the exact original functions. Timers created after it are not recorded; timers recorded before it can still be cleared with clearAll().
  9. Tracking keeps working after clearAll(): new timers are recorded again.

Examples

JAVASCRIPT
const timers = trackTimers();
setTimeout(() => console.log("late"), 1000);
setInterval(() => console.log("tick"), 200);
timers.pendingCount();   // 2
timers.clearAll();       // 2: nothing is ever printed
timers.clearAll();       // 0
timers.restore();
JAVASCRIPT
const timers = trackTimers();
const id = setTimeout(() => {}, 50);
clearTimeout(id);
timers.pendingCount();   // 0: cleared timers are forgotten
setTimeout(() => {}, 10);
// 10 ms later:
timers.pendingCount();   // 0: a timeout that ran is forgotten

Environment

  • Plain JavaScript in a browser page; the tests call the global trackTimers from track-timers.js.
  • The tests install a virtual clock first (it replaces the same four functions and Date.now), then call trackTimers(), so wrap whatever window.setTimeout and friends are at the moment trackTimers() is called.
  • Callbacks are always functions (the string form of setTimeout is not part of this problem). requestAnimationFrame is not tracked.

Hints

  1. Your setTimeout gets an id back from the original only after scheduling. How can the callback, which runs later, still use that id to forget itself?
  2. A recorded interval never "finishes". What is the only way it leaves the record?

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…