@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
35 lines (26 loc) • 648 B
text/typescript
import BackgroundTimer from "./BackgroundTimer";
export class Timer {
private startTime: number;
private timeoutId: number | NodeJS.Timeout | null;
constructor(
private callback: () => void,
private remaining: number = null
) {
this.timeoutId = null;
}
start(): void {
this.startTime = Date.now();
this.timeoutId = BackgroundTimer.setTimeout(this.callback, this.remaining);
this.remaining = 0;
}
pause(): void {
this.clear();
this.remaining -= Date.now() - this.startTime;
}
resume(): void {
this.start();
}
clear(): void {
BackgroundTimer.clearTimeout(this.timeoutId);
}
}