手写节流和防抖
节流
const throttle = (f,time)=>{
let timer = null//计时器
return (...args)=>{
if(timer){return}//如果正在计时,则不能执行
f.call(undefined,...args)
timer = setTimeout(()=>{
timer = null
},time)
}
}
防抖
const debounce = (f,time)=>{
let timer = null
return (...args)=>{
if(timer){
clearTimeout(timer)
}
timer = setTimeout(()=>{
f.call(undefined,...args)
timer = null
},time)
}
}