标签:ttl 重绘 防止 cli his click 操作 size ==
const debounce = (fn, delay = 1000) => {
let timer = null;
return (...args) => {
console.log(`clear timer:`, this.timer); //必须要写this.
console.log(`_that===this :`, _that === this);
clearTimeout(this.timer);
this.timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
console.log(`timer value:`, this.timer);
};
};
<button onclick="debounce(clickFun)()">防抖点击</button>
function clickFun(){
alert(''点击");
}
const throttle=(fn,delay=1000)=>{
let flag=false;
return (...args)=>{
if(this.flag)return;
this.flag=true;
setTimeout(() => {
fn.apply(this,args);
this.flag=false;
}, delay);
}
}
<button onclick="throttle(clickFun)()">节流点击</button>
function clickFun(){
alert(''点击");
}
标签:ttl 重绘 防止 cli his click 操作 size ==
原文地址:https://www.cnblogs.com/M-Silencer/p/11899808.html