Debounce in vanilla JS
The trailing debounce I paste into every project.
Code
Jun 2026
const debounce = (fn, ms = 300) => {
let t;
return (...a) => {
clearTimeout(t);
t = setTimeout(() => fn(...a), ms);
};
};Trailing-edge debounce. For leading-edge, call fn once before starting the timer.
More snippets