防抖
当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定的时间到来之前,再次触发了事件,就重新开始延时。
function debounce(func,delay){
let timer;
return function(){
const context = this;
const args = arguments;
if(timer) clearTimeout(timer);
timer = setTimeout(function(){
func.apply(context,args);
},delay)
}
}