mirror of
https://github.com/FJurmanovic/wallet-web.git
synced 2026-02-06 06:08:10 +00:00
33 lines
701 B
TypeScript
33 lines
701 B
TypeScript
class Timer {
|
|
private timerId: number;
|
|
private start: number;
|
|
private remaining: number;
|
|
private args: any;
|
|
constructor(private callback: () => any, private delay: number, ...args) {
|
|
this.remaining = delay;
|
|
this.args = args;
|
|
this.resume();
|
|
}
|
|
|
|
pause = () => {
|
|
window.clearTimeout(this.timerId);
|
|
this.remaining -= Date.now() - this.start;
|
|
};
|
|
|
|
resume = () => {
|
|
this.start = Date.now();
|
|
window.clearTimeout(this.timerId);
|
|
this.timerId = window.setTimeout(this.callback, this.remaining, ...this.args);
|
|
};
|
|
|
|
reset = (pause: boolean = false) => {
|
|
window.clearTimeout(this.timerId);
|
|
this.remaining = this.delay;
|
|
if (!pause) {
|
|
this.resume();
|
|
}
|
|
};
|
|
}
|
|
|
|
export default Timer;
|