Reputation: 1500
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?
Upvotes: 3
Views: 3268
Reputation: 222518
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');
}
Upvotes: 3