Skip to main content

防抖

function debounce(func, wait, immediate) {
let timeout,self,args,result;
return function (...param) {
self = this;
args = param;
if (timeout) clearTimeout(timeout);
if (immediate) {
// 如果已经执行过,不再执行
let callNow = !timeout;
timeout = setTimeout(function(){
timeout = null;
}, wait)
if (callNow) result = func.apply(self, args)
}
else {
timeout = setTimeout(function(){
func.apply(self, args)
}, wait);
}
return result;
}
}
input.addEventListener('input',debounce(fn,500,false));