Falcon.SugarApi/JS/Debounce.js

25 lines
609 B
JavaScript
Raw Normal View History

2023-08-21 15:16:44 +08:00
/**
* 防抖方法实例化一个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);
}
}
}