javascript 25 lines · 6 steps

Building a debounce function in JavaScript

A closure remembers a pending timer so a function only fires after calls stop arriving.

Explained by highlit
1function debounce(fn, delay) {
2 let timeoutId = null;
3 
4 function debounced(...args) {
5 const context = this;
6 
7 if (timeoutId !== null) {
8 clearTimeout(timeoutId);
9 }
10 
11 timeoutId = setTimeout(() => {
12 timeoutId = null;
13 fn.apply(context, args);
14 }, delay);
15 }
16 
17 debounced.cancel = function () {
18 if (timeoutId !== null) {
19 clearTimeout(timeoutId);
20 timeoutId = null;
21 }
22 };
23 
24 return debounced;
25}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A closure over timeoutId lets each debounced function privately track its own pending timer.
  2. 2Clearing the previous timer on every call ensures fn only runs once activity pauses for delay ms.
  3. 3Capturing this and args at call time, then replaying them via apply, preserves the original calling context.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Building a debounce function in JavaScript — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code