一、防抖封装
多次快速点击,只有一次生效
// 防抖
function antiShake(fn,wait){
let timeOut = null;
return args => {
if(timeOut) clearTimeout(timeOut);
timeOut = setTimeout(fn,wait);
}
}
二、节流封装
在一定时间内只调用一次函数
// 节流
function throttle(fn,wait){
let timeOut = null;
return args => {
if(!timeOut){
timeOut = setTImeout(()=>{
fn();
timeOut = null;
},wait)
}
}
}