class Utils {
lastExecuteTime = 0
// 立即执行 的 debounce 函数:在连续点击delay内执行第一次
immediatelyDebounce(fn, delay = 1000) {
if (+new Date() - delay > this.lastExecuteTime) {
this.lastExecuteTime = +new Date();
return fn;
} else {
this.lastExecuteTime = +new Date();
}
return () => { }
}
// 立即执行 的 throttle 函数:在连续点击,延迟delay执行一次
immediatelyThrottle(fn, delay = 1000) {
if (+new Date() - delay > this.lastExecuteTime) {
this.lastExecuteTime = +new Date();
return fn;
}
return () => { }
}
}
const test = (e) => {
console.log("test", e);
}
const util = new Utils();
const test1 = () => {
util.immediatelyDebounce(test)("immediatelyDebounce");
}
const test2 = () => {
util.immediatelyThrottle(test)("immediatelyThrottle");
}
立即执行的防抖与节流、直接执行函数
于 2023-11-24 16:13:32 首次发布