Debounce Function in JS
Classic debounce — delay invoking fn until X ms after the last call.
Author:
chris
// Language: JavaScript
function debounce(fn, wait = 250) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), wait);
};
}
const onResize = debounce(() => console.log('resized'), 200);
window.addEventListener('resize', onResize);