How to run background JavaScript in Phonegap on iPhone - for timer / stopwatch?

My stopwatch script works flawlessly in the background on Android (counts the time), but iOS 4.3.2 seems to pause the script. On iOS, the stopwatch stops when the app is sent to the background.

I need a runnning stopwatch (time is displayed every time in the view) and some alarm messages at certain points in time.

The stopwatch code is as follows:

this.timer = window.setInterval(function(){ instance.runtime += Stopwatch.INCREMENT; instance.doDisplay(); }, Stopwatch.INCREMENT); 

I know my own solutions, but I would like to use a JavaScript solution for cross-platform compatibility.

+6
source share
1 answer

Since setIntervals do not work in the background, I would use a recursive setTimeout.
This means that you cannot track time with + =, so you will need to use (new date) .getTime (); or Date.now ().

 this.timer = function myTimer () { instance.currentTime = (new Date).getTime(); instance.doDisplay(); setTimeout( myTimer, Stopwatch.INCREMENT ); } 

If setTimeout does not come to life when the application does this, it is easier to restart it.

+6
source

Source: https://habr.com/ru/post/887315/


All Articles