I have to make a timer in Angular, currently i'm doing it by Observable as follows:
timer: Observable<any> = Observable.timer(0, 990);
timerSubscription: Subscription;
this.timerSubscription = this.timer.subscribe((value) => {
this.tickerFunc();
}); // inside ngOnInit
tickerFunc () {
this.timeNow++;
this.timeNowInMinutes = Math.floor(this.timeNow / 60);
if (this.timeNow >= this.TimeLimit) {
this.finishtest();
}
}
So i have two Questions here:
1) Is Using Observable's timer is good, or should i use setInterval() method, or both has the same performance?
2) As you can see, for each time the timer ticks, i have to do two calculations:
first: i have to convert the seconds to minutes.
Second: I have to check if the timer has reached the time limit, and if the case, to unsubscribe the timer.
Both of my questions are related to make a timer as accurate as possible. (as close accurate to real-time as possible). And as you can notice, i have given the tim interval of 990 milliseconds instead of 1000 milliseconds to cover the time loss occured because of the calculations inside the tickerFunc() body.