Falcon.SugarApi/JS/Debounce.js

25 lines
609 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 防抖方法。实例化一个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);
}
}
}