const debounce = function(fn, delay) {
let timer
return function(...args) {
console.log(timer, 'timer')
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn.apply(this, args);
}, delay)
}
}
在input中输入会触发事件handleSearch
handleSearch() {
function test() {
console.log('test', this)
}
const bnfn = debounce(test, 2000)
bnfn() // 每次事件 没有清空上一次的定时器,相当于timer一直为null
}
关于闭包的问题,每次触发事件后的定时器没有被清空
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-