javascript节流函数throttle和debounce

throttle 

function throttle(method, context) {
 clearTimeout(method.tId);
 method.tId= setTimeout(function(){
 method.call(context);
 }, 100);
} 

window.onresize=throttle(func,1000)
window.onscroll=throttle(func,1000)

debounce

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

猜你喜欢

转载自blog.csdn.net/Drifterkiten/article/details/88391111