//节流 经常用在 频繁点击按钮 举例:技能冷却中
function sx(){
console.log(...args)
}//闪现功能
const throttle=(f,time)=>{
let timer=false;
return (...arg)=>{
if(timer) return;
timer=true;
f.call(undefined,...arg);//功能函数放在这里 立即执行
setTimeout(()=>{
// f.call(undefined,...arg);//功能函数放在这里 延后执行
timer=false;
},time*1000)
}
}
const d2=throttle(sx,3)
//防抖 频繁拖动的时候 举例:回城被打断
function hc(...args) {
console.log(...args)
}
const throttle = (f, time) => {
let timer = null;
return (...arg) => {
clearTimeout(timer);
timer = setTimeout(() => {
f.call(undefined, ...arg);
}, time * 1000)
}
}
const d2 = throttle(hc, 3)
【手写节流防抖】
最新推荐文章于 2024-05-31 10:04:55 发布