11.5. Clear every pending timer
Topics
This question is in
11. TimersA 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
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 onwindow, exactly as they were.
Requirements
- After
trackTimers(), the fourwindowfunctions 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. - Every timeout and interval created after
trackTimers()is recorded while it is waiting:pendingCount()counts them. - A timeout that has run is forgotten, even if its callback throws. An interval stays recorded across its runs until it is cleared.
clearTimeoutandclearIntervalforget the timer they cancel. Either one works on either kind of id, as in a browser. An unknown id is ignored.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.- Timers created before
trackTimers()are not recorded andclearAll()leaves them alone. - Timers created inside a timer callback are recorded too.
restore()puts back the exact original functions. Timers created after it are not recorded; timers recorded before it can still be cleared withclearAll().- Tracking keeps working after
clearAll(): new timers are recorded again.
Examples
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();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 forgottenEnvironment
- Plain JavaScript in a browser page; the tests call the global
trackTimersfromtrack-timers.js. - The tests install a virtual clock first (it replaces the same four functions and
Date.now), then calltrackTimers(), so wrap whateverwindow.setTimeoutand friends are at the momenttrackTimers()is called. - Callbacks are always functions (the string form of
setTimeoutis not part of this problem).requestAnimationFrameis not tracked.
Hints
- Your
setTimeoutgets an id back from the original only after scheduling. How can the callback, which runs later, still use that id to forget itself? - 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