aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/utils/Timer.ts
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/frontend/src/utils/Timer.ts')
-rw-r--r--subprojects/frontend/src/utils/Timer.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/subprojects/frontend/src/utils/Timer.ts b/subprojects/frontend/src/utils/Timer.ts
new file mode 100644
index 00000000..8f653070
--- /dev/null
+++ b/subprojects/frontend/src/utils/Timer.ts
@@ -0,0 +1,33 @@
1export class Timer {
2 readonly callback: () => void;
3
4 readonly defaultTimeout: number;
5
6 timeout: number | null = null;
7
8 constructor(callback: () => void, defaultTimeout = 0) {
9 this.callback = () => {
10 this.timeout = null;
11 callback();
12 };
13 this.defaultTimeout = defaultTimeout;
14 }
15
16 schedule(timeout: number | null = null): void {
17 if (this.timeout === null) {
18 this.timeout = setTimeout(this.callback, timeout || this.defaultTimeout);
19 }
20 }
21
22 reschedule(timeout: number | null = null): void {
23 this.cancel();
24 this.schedule(timeout);
25 }
26
27 cancel(): void {
28 if (this.timeout !== null) {
29 clearTimeout(this.timeout);
30 this.timeout = null;
31 }
32 }
33}