blob: efde6633ddfd54a83461325ee43f9bebd06ed010 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
export class Timer {
readonly callback: () => void;
readonly defaultTimeout: number;
timeout: NodeJS.Timeout | null = null;
constructor(callback: () => void, defaultTimeout = 0) {
this.callback = () => {
this.timeout = null;
callback();
};
this.defaultTimeout = defaultTimeout;
}
schedule(timeout: number | null = null): void {
if (this.timeout === null) {
this.timeout = setTimeout(this.callback, timeout || this.defaultTimeout);
}
}
reschedule(timeout: number | null = null): void {
this.cancel();
this.schedule(timeout);
}
cancel(): void {
if (this.timeout !== null) {
clearTimeout(this.timeout);
this.timeout = null;
}
}
}
|