Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent the timer from pausing in the background mode

I have a timer in my Ionic3 application. The timer runs perfectly with a setInterval, but when I close the App into the sleep mode, the timer stops to run. After bringing my App again to the fourground, the timer continue - but he starts at the place where it paused before.

how can I prevent the timer from pausing If I background the App?

My component

time: any;

displayTime() {
  this.time = moment().hour(0).minute(0).second(this.counter++).format('HH : mm : ss');
}

startTime() {
  if(this.runClock == null) {
    this.runClock = setInterval(() => {
      this.displayTime();
    },1000)
  }
}

In my HTML I call {{ time }}.

Plugins like https://ionicframework.com/docs/native/background-mode/ will not work, because the App Store will reject Apps using this plugin.

Any other ideas?

like image 424
asored Avatar asked Dec 21 '25 10:12

asored


1 Answers

Since the application that runs in background mode can be rejected in App Store (and there would be a good reason for this, because nobody needs an app that drains the battery for nothing), this should be done without relying on setInterval counter and background mode:

startMoment = moment();

displayTime() {
  this.time = moment.utc( moment().diff(this.startMoment) ).format('HH : mm : ss');
}
like image 191
Estus Flask Avatar answered Dec 23 '25 23:12

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!