防抖函数:当持续触发事件 一定时间内没有再触发事件 事件处理函数才会执行一次,如果设定的时间到来之前 又一次触发了事件 就重新开始延时
<script>
var input = document.getElementById('input')
function debounce(delay,callback){
let timer
return function(value){
clearTimeout(timer)
timer = setTimeout(function(){
callback(value)
},delay)
}
}
function fn(value){
console.log(value);
}
var debounceFunc = debounce(1000,fn)
input.addEventListener('keyup',function(e){
debounceFunc(e.target.value)
})
</script>