简体   繁体   中英

Ionic 2 Observable.timer destroyed on back button

I have an Observable.timer countdown and when a user leaves the page I want that timer to still be active if they decide to come back later. For example, I only want the timer to be removed when a specific date and time are reached.

I have tried to implement this but when I click back button the timer get destroyed and the endTimer() callback gets called.

TIMER FUNCTION

   countDown: any;
   counter = 1*900;
   tick = 1000;

 newtimer() {
   // create session 
   this.storage.set('dateCreated', 'timer_started');
   // create timer
   this.countDown = Observable.timer(0, this.tick)
      .take(this.counter)
      .map(() => --this.counter).finally(() => this.endTimer()); 
 }

When you leave a page in ionic it will be destroyed. So that timer, which is member var of that page, is destroyed.

If you want that timer keep running, you should put it in some service. Because the service is singleton can will not be destroyed when a page is removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM