Skip to main content

What does "debounce" do?

What debounce is

Debounce (from to debounce, "to remove jitter") is a technique that limits how often a function is called, making it run only after events have stopped for a set amount of time.

In simpler terms:

  • "Wait N milliseconds after the last call, and only then run the function."

Example without debounce

javascript
window.addEventListener('resize', () => { console.log('Window resized'); });

If the user resizes the window, this event can fire dozens of times per second, which places a huge load on the code inside the handler.


Example with debounce

javascript
function debounce(fn, delay) { let timer; return function (...args) { clearTimeout(timer); // reset the previous timer timer = setTimeout(() => fn.apply(this, args), delay); }; } const handleResize = debounce(() => { console.log('Window size changed (after the pause)'); }, 300); window.addEventListener('resize', handleResize);

Now:

  • if the user keeps resizing, the function is not called;
  • as soon as they stop (for >= 300 ms), the function runs once.

Where debounce is used

ScenarioWhy it is needed
scroll, resize, mousemoveThese events fire dozens of times per second
input (search, filter)To avoid making an API request on every keystroke
window.onresizeTo avoid recalculating the layout too often
Autosaving formsSave only after a pause in typing
Live searchSend a request only after the user stops typing

javascript
const input = document.querySelector('#search'); const handleInput = debounce(async (event) => { const query = event.target.value; const results = await fetch(`/api/search?q=${query}`).then(res => res.json()); console.log(results); }, 500); input.addEventListener('input', handleInput);

The user types "frontend", and the request goes out only once, 500 ms after they finish typing.


Debounce variations

1. Only "after the pause" (classic)

  • the function runs once after the stream of events ends.

2. With an immediate call (immediate)

  • the function runs right away on the first event, but then ignores the following ones until delay has passed.
javascript
function debounce(fn, delay, immediate = false) { let timer; return function (...args) { const callNow = immediate && !timer; clearTimeout(timer); timer = setTimeout(() => { timer = null; if (!immediate) fn.apply(this, args); }, delay); if (callNow) fn.apply(this, args); }; }

Debounce vs Throttle

PropertyDebounceThrottle
When it is calledAfter a pauseEvery N ms
Call frequencyAt most once after "quiet time"Regularly, but not more than every N ms
Typical caseinput, resize, searchscroll, mousemove, infinite scroll
Example behaviorFires once after actions finishFires every N ms while actions continue

Summary

What it doesWhy it is needed
Delays a function call until a series of events endsPrevents excessive calls
Reduces CPU and network loadImproves responsiveness and stability
Simple to implement and integrateWorks with any event handlers
Often used together with throttleFor different scenarios

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.