防抖
一定时间内多次触发事件只执行最后一次
function debounce(fn,delay){
let timeId = null
return function(){
if(timeId){
clearTimeout(timeId)
}
timeId = setTimeout(()=>{
fn.call(this)
},delay)
}
}
节流
规定时间内只执行一次
//节流
function throttle(fn, delay) {
let flag = true;
return function () {
if (flag) {
setTimeout(() => {
fn.call(this);
flag = true;
}, delay);
flag = false;
}
};
}
网友评论