maoosi
8/13/2015 - 11:43 AM

Simple JavaScript throttle function - From Grafikart https://www.grafikart.fr/tutoriels/javascript/debounce-throttle-642

Simple JavaScript throttle function - From Grafikart https://www.grafikart.fr/tutoriels/javascript/debounce-throttle-642

function throttle(callback, delay) {
    var last;
    var timer;
    return function () {
        var context = this;
        var now = +new Date();
        var args = arguments;
        if (last && now < last + delay) {
            clearTimeout(timer);
            timer = setTimeout(function () {
                last = now;
                callback.apply(context, args);
            }, delay);
        } else {
            last = now;
            callback.apply(context, args);
        }
    };
}