增加防抖函数Debounce
This commit is contained in:
parent
459dcf8548
commit
8d58cb43b3
25
JS/Debounce.js
Normal file
25
JS/Debounce.js
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user