I created a Observable.timer in my Ionic 2 project to countdown the time (e.g. 00:00:59->00:00:00). For example, I created the timer in a page called "PageB" and below is the typescript code:
export class PageB {
constructor(){
this.timer = Observable.timer(500,500).map(i=>this.result-i*503).take((this.result-575999970)/503).subscribe(i =>
{
this.time=i;
console.log(i);
console.log(moment(this.time).format('HH:mm:ss'));
});
}
}
Console.log output for the above code:
However, I got a problem that this timer will be duplicated when I leave from PageB to previous page and then go back to PageB again (PageB->previous page->PageB).
The duplicated situation:
You can see that 00:00:47, 00:00:46 and the rest are duplicated so many times. I cannot use "this.timer.unsubscribe()"
to stop the timer when leaving PageB because I need it keep counting down in the background.
Therefore, could anyone tell me how to solve this problem? thanks