function debounce(fn, delay) { // fn = test
let timerId = null;
return function () {
let self = this;
let args = arguments;
timerId && clearTimeout(timerId);
timerId = setTimeout(function () {
fn.apply(self, args);
}, delay || 1000);
}
}
function throttle(fn, delay) { // fn = test
let timerId = null;
let flag = true;
return function () {
if(!flag) return;
flag = false;
let self = this;
let args = arguments;
timerId && clearTimeout(timerId);
timerId = setTimeout(function () {
flag = true;
fn.apply(self, args);
}, delay || 1000);
}
}
函数防抖和函数节流
最新推荐文章于 2024-06-03 19:31:31 发布