Enjoy 20% off all plans by following us on social media. Check out other promotions!

Debounce

Author
Ex-Meta Staff Engineer
Languages
JSTS

Debouncing is a technique used to control how many times we allow a function to be executed over time. When a JavaScript function is debounced with a wait time of X milliseconds, it must wait until after X milliseconds have elapsed since the debounced function was last called. You almost certainly have encountered debouncing in your daily lives before — when entering an elevator. Only after X duration of not pressing the "Door open" button (the debounced function not being called) will the elevator door actually close (the callback function is executed).

Implement a debounce function which accepts a callback function and a wait duration. Calling debounce() returns a function which has debounced invocations of the callback function following the behavior described above.

Examples

let i = 0;
function increment() {
i++;
}
const debouncedIncrement = debounce(increment, 100);
// t = 0: Call debouncedIncrement().
debouncedIncrement(); // i = 0
// t = 50: i is still 0 because 100ms have not passed.
// t = 100: increment() was invoked and i is now 1.

debouncedIncrement() is called multiple times.

let i = 0;
function increment() {
i++;
}
const debouncedIncrement = debounce(increment, 100);
// t = 0: Call debouncedIncrement().
debouncedIncrement(); // i = 0
// t = 50: i is still 0 because 100ms have not passed.
// Call debouncedIncrement() again.
debouncedIncrement(); // i = 0
// t = 100: i is still 0 because it has only
// been 50ms since the last debouncedIncrement() at t = 50.
// t = 150: Because 100ms have passed since
// the last debouncedIncrement() at t = 50,
// increment was invoked and i is now 1 .

Follow Up

  • Debounce with a cancel() method to cancel delayed invocations and a flush() method to immediately invoke them.
  • Implement throttle, which is similar to debounce but a little different.

Reading

Companies

Premium FeaturePurchase premium to see companies which ask this question.
View plans