Throttle 小例子


function testFn() { console.log(1111); }
var throttle = function (fn, delay,atleast) {
    var timer = null;
    var pretime = null;
    return function () {
        var now = Date.now()
        if(!pretime) pretime = now;
        if(now - pretime >atleast){
            fn();
            pretime = now;
        }else{
            clearTimeout(timer);
            timer = setTimeout(function() {
                fn();
            }, delay);
        }
    }
};
throttle(testFn, 200,1000)()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章