Debouncing delays a function call until a burst of events has stopped for a set time. Every new event restarts the timer, so the function runs once, after the user pauses typing, resizing or scrolling. In JavaScript you build it with setTimeout and clearTimeout inside a closure.
Without debouncing, an input handler that calls an API fires on every keystroke. With it, typing "javascript" sends one request instead of ten.
A debounced function wraps your original function and keeps a single timer ID in a closure. The flow is:
The result is that the function runs only after the activity has been quiet for the full delay.
The returned debounced function is a regular function, not an arrow function, so it keeps the caller's this and passes it to fn through apply. The cancel method lets you drop a pending call, which matters when a component is removed before the timer fires.
The most common use is live search. The handler below waits 400 ms after the last keystroke before it calls the API:
Debouncing reduces how many requests start, and AbortController cancels a request that is still in flight when a newer one begins. Together they prevent an older, slower response from overwriting a newer one. Using textContent instead of innerHTML also avoids injecting untrusted markup.
The version above runs on the trailing edge: after the pause. Sometimes you want the first call to run immediately and ignore the rest of the burst, such as a "Save" button that should respond at once but not fire twice.
With leading: true and trailing: false, the first click runs immediately and later clicks inside the one-second window are ignored. If you need these options plus maxWait, a maintained utility such as Lodash's debounce handles the edge cases for you.
Debounce and throttle both limit how often a function runs, but they answer different questions.
| Technique | Runs when | Good for |
|---|---|---|
| Debounce | After events stop for the delay | Search-as-you-type, auto-save, validating a field, final window size |
| Throttle | At most once per interval while events continue | Scroll position tracking, drag handling, mouse-move analytics |
Use debounce when only the final value matters. Use throttle when you need regular updates during a continuous action. For visual updates tied to rendering, such as animating on scroll, requestAnimationFrame is often a better fit than either.
In a component, the debounced function must keep the same identity between renders, otherwise each render creates a new timer and nothing is debounced. Create it once and clean up on unmount:
If onSearch changes on every render, wrap it in useCallback in the parent so the memoized debounce is not recreated each time.
this by using an arrow function as the wrapper when the original function depends on it.Debounce waits until events stop and then runs once. Throttle runs at a steady maximum rate while events continue.
For search inputs, 250 to 500 ms is a common range. Pick the shortest delay that still stops redundant work, and test it with real typing speed.
No. The language and browsers do not provide one, so you write a small helper like the one above or use a utility library.
Yes, but the debounced wrapper returns immediately rather than the result. Handle the result inside the wrapped function, and use AbortController to cancel stale requests.
Debouncing postpones a function until activity pauses, using a closure that holds a setTimeout timer. Use the trailing edge for search and auto-save, the leading edge for instant-response buttons, and throttle when you need updates during continuous input. Create the debounced function once, cancel it on cleanup, and pair it with AbortController for network calls.