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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A closure over timeoutId lets each debounced function privately track its own pending timer.
- 2Clearing the previous timer on every call ensures fn only runs once activity pauses for delay ms.
- 3Capturing this and args at call time, then replaying them via apply, preserves the original calling context.
Related explainers
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
typescript
import { Pipe, PipeTransform, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core'; @Pipe({ name: 'timeAgo',
A self-refreshing timeAgo pipe in Angular
impure-pipe
change-detection
timers
Advanced
10 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
rust
use std::collections::HashMap; pub struct Memoizer<K, V, F> { cache: HashMap<K, V>,
A generic memoizer in Rust
memoization
generics
caching
Intermediate
6 steps
javascript
const transitions = { cart: { checkout: 'shipping' }, shipping: { submitAddress: 'payment', back: 'cart' }, payment: { submitPayment: 'review', back: 'shipping' },
A finite state machine for checkout flow
state-machine
event-driven
data-driven-design
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-debounce-function-in-javascript-explained-javascript-8266/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.