You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
715 B
30 lines
715 B
8 months ago
|
export class Debounce {
|
||
|
private delay: number;
|
||
|
private timeoutID: number | null;
|
||
|
private lastExec: number;
|
||
|
private callback: Function;
|
||
|
private args?: any[];
|
||
|
|
||
|
constructor(delay: number, callback: Function) {
|
||
|
this.delay = delay;
|
||
|
this.lastExec = 0;
|
||
|
this.timeoutID = null;
|
||
|
this.callback = callback;
|
||
|
}
|
||
|
|
||
|
exec(...args: any[]) {
|
||
|
if (this.timeoutID) {
|
||
|
// console.log("clear id" + this.timeoutID);
|
||
|
clearTimeout(this.timeoutID);
|
||
|
}
|
||
|
// this.args = args;
|
||
|
this.timeoutID = setTimeout(() => {
|
||
|
this.callback(args);
|
||
|
}, this.delay);
|
||
|
}
|
||
|
|
||
|
run() {
|
||
|
this.callback(this.args);
|
||
|
}
|
||
|
}
|