防抖和节流函数实现
1.手写防抖
function debounce(handleEvent, time, flag){
let timeout = null;
return function(...agrs){
clearTimeout(timeout)
if(flag && !timeout){
handleEvent.apply(this, args)
}
timeout = setTimeout(()=>{
handleEvent.apply(this, args)
},time)
}
}
2.手写节流
function throttle(handleEvent, time){
let timer = null;
return function(...args){
if(!timer){
timer = setTimeout(() => {
timer = null;
handleEvent.apply(this, args)
}, time);
}
}
}