← Back to Snippets
JavaScriptMay 2026
Debounce function
Delays a function call until a given wait period has passed — ideal for search inputs and resize events.
snippet
function debounce(fn, wait = 300) {
let timer
return function (...args) {
clearTimeout(timer)
timer = setTimeout(() => fn.apply(this, args), wait)
}
}
// Usage — debounce a search handler
const handleSearch = debounce((query) => {
console.log('Searching for:', query)
}, 400)
input.addEventListener('input', (e) => handleSearch(e.target.value))PerformanceEventsUtility