Debounce Function in Javascript 🚀

Cagatay Unal - Sep 17 '21 - - Dev Community
let count = 0;

const debounce = (func, delay) => {
    let timer;
    return function(){
        clearTimeout(timer);
        timer = setTimeout(() => {
            func();
        }, delay);
    }
}

let scrollCount = () => {
    console.log(count++);
}

scrollCount = debounce(scrollCount, 500);

window.addEventListener('scroll', scrollCount);
Enter fullscreen mode Exit fullscreen mode
. . . . .