</> Snippet
NAVIGATION
HomeSnippets
ACTIONS
JavaScript#javascript#utils

Debounce helper

Delay a function call until input stops for a given interval.

26d ago·136 views·10 lines
debounce.js10 lines
Raw Download
1export function debounce(fn, wait = 300) {
2 let timer;
3 return (...args) => {
4 clearTimeout(timer);
5 timer = setTimeout(() => fn(...args), wait);
6 };
7}
8
9// usage: const onSearch = debounce((q) => console.log(q), 400);
10
README.md

Debounce

Useful for search inputs and resize handlers — the wrapped function only runs once the caller has been quiet for wait milliseconds.