Debounce a Function
Debounce a Function is a reusable JavaScript snippet. Creates a debounced version of a function that delays invocation until after `delay` milliseconds have elapsed since the last call. Useful for search inputs and resize handlers. The full implementation: function debounce(fn, delay = 300) { let timer return function(...args) { clearTimeout(timer) timer = setTimeout(() => fn.apply(this, args), delay) } } // Usage const handleSearch = debounce((query) => { fetchResults(query) }, 500) This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.