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