const input1 = document.getElementById('input1')
function debounce (fn, delay = 500) {
let timer = null
return function () {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this,arguments)
timer = null //释放空间
}, delay)
}
}
input1.addEventListener('keyup', debounce(function () {
console.log(input1.value)
}, 1000))
手写防抖debounce
最新推荐文章于 2024-08-08 00:30:00 发布