mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
25 lines
431 B
TypeScript
25 lines
431 B
TypeScript
export class Debounce {
|
|
private timer: number | null = null;
|
|
private readonly delay: number;
|
|
|
|
constructor(delay: number) {
|
|
this.delay = delay;
|
|
}
|
|
|
|
run(callback: () => void) {
|
|
if (this.timer) {
|
|
clearTimeout(this.timer);
|
|
}
|
|
this.timer = setTimeout(() => {
|
|
callback();
|
|
}, this.delay);
|
|
}
|
|
|
|
cancel() {
|
|
if (this.timer) {
|
|
clearTimeout(this.timer);
|
|
this.timer = null;
|
|
}
|
|
}
|
|
}
|