Skip to curriculum
Jump to the editor

15.1. Debounce

Medium
Topics

This question is in

15. Debounce and throttle
15–25 min

A search box sends a request on every keystroke. Type "react" and five requests leave, though only the last one matters. Debouncing fixes that: the work waits until the calls stop for a while, then runs once.

Write debounce(fn, wait) in debounce.js. It returns a new function. Each call to that function restarts a wait of wait milliseconds; when a whole wait passes with no new call, fn runs once, with the this and the arguments of the last call.

Signature

JAVASCRIPT
function debounce(fn, wait) // returns a function
  • fn: the function to run.
  • wait: milliseconds of quiet before fn runs. A missing wait counts as 0.
  • The returned function returns undefined: fn has not run yet when it returns.

Requirements

  1. debounce returns a function, and calling it does not call fn straight away.
  2. After a single call, fn runs exactly wait ms later: not 1 ms earlier.
  3. A burst of calls runs fn once, wait ms after the last call. A call during the wait restarts it.
  4. fn receives every argument of the last call, and the this of the last call (so it works as an object's method).
  5. Each function that debounce returns has its own timer: debouncing two handlers never cancels one with the other.
  6. After fn runs, the next call starts a new wait, so a second burst runs fn again.
  7. Only one timer is ever waiting, and none is left once fn has run.
  8. With wait 0, fn still runs in a later timer task, never during the call.

Examples

JAVASCRIPT
const log = debounce((text) => console.log("search:", text), 300);

log("r");     // at 0 ms
log("re");    // at 100 ms: the wait starts again
log("rea");   // at 200 ms: the wait starts again
// at 500 ms (200 + 300): "search: rea", once
JAVASCRIPT
const box = {
  name: "box",
  save: debounce(function (value) { console.log(this.name, value); }, 100),
};

box.save(1);
box.save(2);
// 100 ms later: "box 2"
JAVASCRIPT
const a = debounce(() => console.log("a"), 100);
const b = debounce(() => console.log("b"), 100);
a();   // at 0 ms
b();   // at 50 ms
// "a" at 100 ms, "b" at 150 ms: separate timers

Environment

  • Plain JavaScript in a browser page; the tests call the global debounce from debounce.js.
  • The tests run on a virtual clock that replaces setTimeout and clearTimeout, so use those (no requestAnimationFrame, no busy loops).
  • Leading-edge calls, maxWait, cancel and flush are not part of this problem: they are problem 15.2.

Hints

  1. The timer has to survive between calls, but belong to one debounced function. Where can a variable live so that happens?
  2. Arrow functions take this from where they are written. Which kind of function gets the caller's this?

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…