forxtu
8/24/2017 - 9:52 PM

throttle function scroll

throttle function scroll

//Create the listener function
function throttle (callback, limit) {
    var wait = false;                 // Initially, we're not waiting
    return function () {              // We return a throttled function
        if (!wait) {                  // If we're not waiting
            callback.call();          // Execute users function
            wait = true;              // Prevent future invocations
            setTimeout(function () {  // After a period of time
                wait = false;         // And allow future invocations
            }, limit);
        }
    }
 }
 function callback () {
    console.count("Throttled");
}

window.addEventListener("scroll", throttle( callback, 200 ));