Skip to curriculum
Jump to the editor

11.3. A cancellable timeout and interval

Easy
Topics

This question is in

11. Timers
10–20 min

Every timer needs a way to stop it: a toast that is dismissed before it hides itself, a polling loop that ends when the user leaves the page, a component that is removed while its timer is still waiting. The built-ins hand back an id, and you have to keep it and pass it to the right clear function. Many codebases wrap that in a helper that returns a cancel function instead: the caller does not need to know which timer is underneath, and "stop this" is one call.

Write timeout(fn, delay, ...args) and interval(fn, delay, ...args) in cancellable.js. Each starts its timer at once and returns a cancel function.

Signature

JAVASCRIPT
function timeout(fn, delay, ...args)  // returns cancel()
function interval(fn, delay, ...args) // returns cancel()
  • fn: the function to run, called with args. delay: milliseconds.
  • cancel() takes no arguments and returns true if it stopped something that was still going, false if there was nothing left to stop.

Requirements

timeout

  1. fn(...args) runs once, exactly delay ms after the call, never during the call (even with delay 0).
  2. cancel() before it runs stops it for good: fn never runs and no timer is left waiting. It returns true.
  3. cancel() after fn has run, or a second cancel(), does nothing and returns false.

interval

  1. fn(...args) runs every delay ms: at delay, 2 × delay, 3 × delay…, with the same arguments each time.
  2. cancel() stops it: no more runs and no timer left waiting. The first call returns true, every later call false.
  3. cancel() works from inside fn: the current run finishes, and there is no next one.
  4. cancel() before the first run means fn never runs.

both

  1. Every call has its own timer and its own cancel: canceling one never affects another.

Examples

JAVASCRIPT
const cancel = timeout(console.log, 1000, "hello");
cancel();   // true: "hello" is never printed
cancel();   // false: nothing left to stop
JAVASCRIPT
let ticks = 0;
const stop = interval(() => {
  ticks += 1;
  if (ticks === 3) stop();   // stops itself after the third run
}, 500);
// runs at 500, 1000 and 1500 ms, then never again
JAVASCRIPT
const cancel = timeout(() => console.log("done"), 0);
// later, after "done" was printed:
cancel();   // false: it already ran

Environment

  • Plain JavaScript in a browser page; the tests call the globals timeout and interval from cancellable.js.
  • The tests run on a virtual clock that replaces setTimeout, setInterval and their clear functions; use those.
  • What fn returns is not used. fn throwing is not part of this problem.

Hints

  1. Where does the timer id live so that cancel can still reach it after timeout has returned?
  2. How does cancel know whether the timeout already ran?

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…