Delays execution of a function until after a specified wait time has elapsed since the last call. Useful for search inputs and resize events.
1function debounce(fn, wait) {
2 let timer;
3 return (...args) => {
4 clearTimeout(timer);
5 timer = setTimeout(() => fn(...args), wait);
6 };
7}
8
9// usage
10const onSearch = debounce((query) => {
11 fetchResults(query);
12}, 300);