首页 > 如何给resize事件添加节流功能

如何给resize事件添加节流功能

现在有一个需求,在$(window).resize时clearInterval(xxx),然后再resize结束时再给xxx=setInterval(function(){},xxx),如何才能实现?


var intervalId, timeoutId, interval = 500;
$(window).resize(function(){
    if(intervalId){
        clearInterval(xxx);
    }
    
    timeoutId && clearTimeout(timeoutId);
    
    if(!intervalId){
        // 如果 500 毫秒内没有执行 resize 事件我们就认为停止 resize 啦
        timeoutId = setTimeout(function(){
           intervalId = setInterval(function(){}, milliseconds);
        }, interval)
    }
    
});

var handle;
function resize(e) {
    clearInterval(handle);
    handle = setInterval(function(){
        ...dosomething
    }, 100);
}
【热门文章】
【热门文章】