Skip to curriculum
Jump to the editor

11.4. An interval you can pause and resume

Medium
Topics

This question is in

11. Timers
20–30 min

A slideshow advances every 5 seconds and pauses while the pointer is over it. A game ticks every second and stops while the menu is open. With a plain setInterval, pausing means clearing it, and resuming means starting a new one, which waits a full period again: pause 4 seconds into a 5-second slide, resume, and the slide stays up for 9 seconds. A pausable interval remembers how much of the wait was left.

Write createPausableInterval(fn, delay) in pausable-interval.js. It starts at once and returns three controls: pause, resume and stop.

Signature

JAVASCRIPT
function createPausableInterval(fn, delay) // returns { pause, resume, stop }
  • fn(run): called on every run with the run number, 1, 2, 3…
  • delay: milliseconds between runs (a positive integer).
  • pause(), resume() and stop() take no arguments and return undefined.

Requirements

  1. It starts straight away: fn runs at delay, 2 × delay, 3 × delay… after the call, never earlier. fn receives the run number, which keeps counting across pauses.
  2. pause() stops the runs and remembers how much of the current wait was left. While paused, fn never runs and no timer is waiting.
  3. resume() waits only for the time that was left, runs fn, and then goes back to running every delay ms. Paused right after a run, the next run comes a full delay after resume().
  4. pause() while already paused does nothing (the time left stays what it was at the first pause). resume() while running does nothing (no extra runs, no change to the schedule).
  5. stop() ends it for good: no more runs, no timer left, and a later resume() does nothing. It works while running and while paused.
  6. pause() and stop() work from inside fn.
  7. At most one timer is ever waiting.
  8. Each pausable interval is independent of the others.

Examples

JAVASCRIPT
const slides = createPausableInterval((n) => console.log("slide", n), 1000);
// 1000 ms: slide 1
// 1400 ms: slides.pause()   (600 ms of the wait were left)
// 5000 ms: slides.resume()
// 5600 ms: slide 2          (only the 600 ms that were left)
// 6600 ms: slide 3          (a full 1000 ms again)
JAVASCRIPT
const ticker = createPausableInterval((n) => {
  if (n === 3) ticker.stop();
}, 200);
// runs at 200, 400 and 600 ms, then never again
JAVASCRIPT
const a = createPausableInterval(console.log, 100);
a.resume();   // does nothing: it is not paused
a.pause();
a.pause();    // does nothing: already paused

Environment

  • Plain JavaScript in a browser page; the tests call the global createPausableInterval from pausable-interval.js.
  • The tests run on a virtual clock that replaces setTimeout, setInterval, their clear functions, Date.now and performance.now; use those to schedule and to measure time.
  • fn throwing an error is not part of this problem.

Hints

  1. To know how much of the wait is left at the moment of pause(), what do you need to have written down when the wait began?
  2. Which three states can the interval be in, and which control is allowed in each?

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…