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)