//防抖
function debounce(fn, delay) {
var timer = null;
// return function () {
// if (timer) clearTimeout(timer);
// var _this = this;
// var _arguments = arguments;
// timer = setTimeout(function () {
// fn.apply(_this,_arguments);
// }, delay);
// }
return (...arg)=>{
if (timer) clearTimeout(timer);
timer=setTimeout(()=>{
fn.apply(this,arg);
},delay)
}
}
//节流
function throttle(fn,delay){
var flag = true;
// return function (e){
// if(flag){
// setTimeout(() => {
// //到规定时间后执行函数,同时flag=true
// fn(this,arguments);
// flag = true;
// },delay);
// }
// //防止一直执行
// flag = false;
// };
return (...args)=>{
if(flag){
setTimeout(()=>{
fn(this,args);
flag=true
},delay)
}
flag=false
}
}
js防抖和节流函数
最新推荐文章于 2023-04-10 10:05:39 发布