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)