防抖
const debounce = (fn,time)=>{
let timer = null
return ()=>{
if(timer){
clearTimeout(timer)
}
timer = setTimeout(()=>{
fn()
timer = null
},time)
}
}
//使用
const tp = debounce(()=>{
console.log('tp了')
},3000)
节流
const throttle = (fn, time) => {
let timer = null;
return (...args) => {
if (timer) {
return;
}
fn.call(undefined,...args)
timer = setTimeout(() => {
timer = null
}, time);
};
};
//使用
const coodown = throttle(()=>{
console.log('cd 中')
},3000)