189
Top Answer
Classic debounce implementation:
function debounce(func, wait) {
let timeoutId = null;
return function(...args) {
// Clear previous timer
clearTimeout(timeoutId);
// Set new timer
timeoutId = setTimeout(() => {
func.apply(this, args);
}, wait);
};
}
// Usage
const debouncedSearch = debounce((query) => {
console.log('Searching for:', query);
}, 300);
input.addEventListener('input', (e) => {
debouncedSearch(e.target.value);
});
The function only executes after the user stops typing for 300ms.
UtilityMaster