window resize和scroll事件的基本优化 – WEB前端开发 Web前端开发 • 11年前 扫码分享 我是创始人李岩:很抱歉!给自己产品做个广告,点击进来看看。 maybe a better solution? function debounce(func, wait, immediate) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if (!immediate) func.apply(context, args); }; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; } $(window).on(‘resize’, debounce(function(events){ console.log("window resize"); }, 400)); [回复] 随意打赏
maybe a better solution?
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
$(window).on(‘resize’, debounce(function(events){
console.log("window resize");
}, 400));
[回复]