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

后端 未结 1 581
长发绾君心
长发绾君心 2021-02-02 04:18

My stopwatch script runs flawlessly in the background on Android (counting up the time), but iOS 4.3.2 seems to pause the script. On iOS the stopwatch stops at the time where th

1条回答
  •  庸人自扰
    2021-02-02 04:35

    Since setIntervals don't work in the background I'd use a recursive setTimeout instead.
    Which means you can't keep track of time with a +=, so you'll need to use a (new Date).getTime(); or Date.now() instead.

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

    If the setTimeout doesn't come back to life when the app does it should be simpler to restart.

    0 讨论(0)
提交回复
热议问题