throttle和debounce简单实现

function debounce(delay,fn){
    var timer;
    return function(){
        var ctx = this,args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function(){
            fn.apply(ctx,args);
        },delay);
    }
}

function throttle(interval,fn){
    var last = 0;
    return function(){
        var cur = +new Date(),ctx = this,args = arguments;
        if(cur - last > interval){
            fn.apply(ctx,args);
            last = cur;
        }
    }
}

 

 

参考:http://www.cnblogs.com/fsjohnhuang/p/4147810.html

posted @ 2016-12-19 18:14  全玉  阅读(440)  评论(0编辑  收藏  举报