// 定义 : 当持续的触发事件 一定事件内没有再次触发事件 事件处理函数才会执行一次
var input = document.getElementById('input')
function debounce(delay){
let timer
return function ( value){
clearTimeout(timer)
timer = setTimeout( function(){
console.log(value) // 如果打印结果不想在这里输出 而是在外面输出 那么久需要一个回调函数
},delay)
}
}
var debounceFunc = debounce(1000)
input.addEventListener('keyup',function(e){
debounceFunc(e.target.value)
})
// 分割线
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)
})
07-19
170
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-20
396
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-18
373
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-18
1024
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交