DEV Community

Cover image for Debounce Function in Javascript πŸš€
Cagatay Unal
Cagatay Unal

Posted on

Debounce Function in Javascript πŸš€

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

Top comments (0)