/
Timer.ts
34 lines (29 loc) · 827 Bytes
/
Timer.ts
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
34
import { ITimer } from './ITimer';
export class Timer implements ITimer {
public AutoReset = false;
public Enabled = false;
public Interval: number;
public Elapsed = new Array<() => void>();
constructor(interval?: number)
constructor(interval?: any) {
interval ? this.Interval = interval : this.Interval = 0;
}
public async Start(): Promise<void> {
return await new Promise<void>((resolve) => {
this.Enabled = this.Elapsed.length >= 1;
const executer = setInterval(async () => {
this.Elapsed.forEach(item => item());
if (!this.AutoReset) {
this.Enabled = false;
}
if (!this.Enabled) {
clearInterval(executer);
resolve();
}
}, this.Interval);
});
}
public Stop(): void {
this.Enabled = false;
}
}