typestar

Debounce in JavaScript

Waits for the calls to stop before doing anything, which is what a search box wants.

function debounce(fn, wait) {
  let timer = null;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, wait);
  };
}

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
160
Tokens
50
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 29 seconds.

Type this snippet

Step 4 of 5 in Composing functions, step 8 of 16 in Functions & patterns.

← Previous Next →