JavaScript

Debounce

by @admin
10h ago
Apr 28, 2026
Public
Delays invoking a function until after a specified wait time has elapsed since the last time it was called. Essential for search inputs, resize handlers, and any event that fires rapidly — prevents excessive function calls and improves performance.
JavaScript
function debounce(fn, wait = 300) {
  let timer;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), wait);
  };
}

// Usage
const onSearch = debounce((e) => {
  console.log('Searching:', e.target.value);
}, 400);

document.querySelector('#search').addEventListener('input', onSearch);
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.