File size: 2,063 Bytes
3c491ea | 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | export class RoundTimeTimer {
private roundTime: number;
private intervalMs: number = 1000;
private intervalTimer: any;
private timeOutFunction: Function | null = null;
private remainingTime: number = 0;
private intervalFunction: ((remainingTime: number) => void) | null = null;
private isPause = false;
constructor(roundTime: number, intervalMs = 1000) {
this.roundTime = roundTime;
this.intervalMs = intervalMs;
this.remainingTime = roundTime;
}
public async start(callback: Function | null, timeS: number = this.roundTime): Promise<void> {
this.remainingTime = timeS * 1000;
if (callback) this.timeOutFunction = callback;
this.clearInterval();
return new Promise((resolve) => {
this.intervalTimer = setInterval(() => {
if (!this.isPause) this.runIntervalFunction();
if (this.remainingTime <= 0) {
this.runTimeOutFunction();
this.clearInterval();
resolve();
}
if (!this.isPause) this.remainingTime -= this.intervalMs;
}, this.intervalMs);
});
}
public nextTick() {}
public pause() {
this.isPause = true;
}
public resume() {
this.isPause = false;
}
public stop() {
this.clearInterval();
}
public async setTimeOutFunction(newFunction: Function | null) {
this.intervalFunction && this.setIntervalFunction(this.intervalFunction);
await this.start(newFunction);
}
public setIntervalFunction(countDownCallback: (remainingTime: number) => void | null) {
this.intervalFunction = countDownCallback;
// this.clearInterval();
// if (!this.isPause) {
// this.intervalTimer = setInterval(() => {
// this.runIntervalFunction();
// }, this.intervalMs);
// }
}
private runIntervalFunction() {
this.intervalFunction && this.intervalFunction(Math.round(this.remainingTime / 1000));
}
private runTimeOutFunction() {
if (!this.timeOutFunction) return;
this.timeOutFunction();
}
public clearInterval() {
if (this.intervalTimer) {
clearInterval(this.intervalTimer);
this.intervalTimer = null;
}
}
public destroy() {
this.clearInterval();
}
}
|