手写一个防抖
防抖和节流都是依托定时器来完成的
let timer=null
input1.addEventLister('keyup',function(){
if(timer){
clearTimeout(timer)
}
timer=setTimeout(()=>{
console.log(123)
timer=null
},1000)
})
防抖的封装
let timer=null
return function(){
if(timer){
clearTimeout(timer)
}
timer=setTimeout(()=>{
console.log(123)
timer=null
},delay)
}
}
input.addEventListener('keyup',debounce(()=>{console.log(123)},600))
标签:防抖,console,log,timer,123,手写,null
From: https://www.cnblogs.com/cmycmy/p/17015246.html