blob: 4bb1bb9c7de5e7f5ac0e9a0facf57beb82337aed (
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 default class Timer {
private readonly callback: () => void;
private readonly defaultTimeout: number;
private timeout: number | undefined;
constructor(callback: () => void, defaultTimeout = 0) {
this.callback = () => {
this.timeout = undefined;
callback();
};
this.defaultTimeout = defaultTimeout;
}
schedule(timeout?: number | undefined): void {
if (this.timeout === undefined) {
this.timeout = setTimeout(this.callback, timeout ?? this.defaultTimeout);
}
}
reschedule(timeout?: number | undefined): void {
this.cancel();
this.schedule(timeout);
}
cancel(): void {
if (this.timeout !== undefined) {
clearTimeout(this.timeout);
this.timeout = undefined;
}
}
}
|