function debounce(fn,delay){
let timer =null;
return function(){
let context = this, arg = arguments;
clearTimeout(timer); //清除延迟器
timer = setTimeout(function(){
fn.apply(context,arg);//绑定函数
},delay);
}
}
//绑定需要节流的函数 返回值是一个函数
this.println = debounce(println,300);
//监听inputChange方法进行节流
function changeValue(value){
this.println(value)
}
//需要被节流的功能函数
function println(value){
console.log(value)
}
//oninput 输入会时时触发
<input type="text" value="" oninput='changeValue(this.value)'/>
手写防抖函数
最新推荐文章于 2024-08-10 21:46:38 发布