增加防抖函数Debounce

This commit is contained in:
吴长征 2023-08-21 15:16:44 +08:00
parent 459dcf8548
commit 8d58cb43b3

25
JS/Debounce.js Normal file
View File

@ -0,0 +1,25 @@
/**
* 防抖方法实例化一个Debounce对象然后调用run方法执行防抖函数
*/
class Debounce {
constructor(time) {
//执行间隔
this.timeout = time;
//计时器
this.timer = null;
}
//等待时间结束后执行方法
run(func, ...args) {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
if (this.timer == null) {
this.timer = setTimeout(() => {
func(...args);
this.timer = null;
}, this.timeout);
}
}
}