admin 管理员组文章数量: 1086019
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 the application was sent to background.
I need a runnning stopwatch (the time appears updated each second in the view) and some alarm messages in defined points in time.
The code for the stopwatch is as below:
this.timer = window.setInterval(function(){
instance.runtime += Stopwatch.INCREMENT;
instance.doDisplay();
}, Stopwatch.INCREMENT);
I'm aware of native solutions, but would like to stick with a JavaScript solution for the sake of cross-platform patibility.
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 the application was sent to background.
I need a runnning stopwatch (the time appears updated each second in the view) and some alarm messages in defined points in time.
The code for the stopwatch is as below:
this.timer = window.setInterval(function(){
instance.runtime += Stopwatch.INCREMENT;
instance.doDisplay();
}, Stopwatch.INCREMENT);
I'm aware of native solutions, but would like to stick with a JavaScript solution for the sake of cross-platform patibility.
Share Improve this question asked May 4, 2011 at 22:08 Michael SchmidtMichael Schmidt 3,5215 gold badges35 silver badges39 bronze badges 2- Hey I've been searching all over for detecting that an app is running in the background. Can you give me a remendation? – xdumaine Commented Jan 26, 2012 at 14:53
- Does it work accurately on Android, I've tried making code that runs every 5 seconds, it works in background, but not in 5 seconds fixed interval, I think this is because the CPU sleeps, so Did you made it accurate ? How ? – Pola Edward Commented Jan 19, 2014 at 18:58
1 Answer
Reset to default 6Since 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 e back to life when the app does it should be simpler to restart.
本文标签: How to run a background JavaScript in Phonegap on iPhonefor timerstopwatchStack Overflow
版权声明:本文标题:How to run a background JavaScript in Phonegap on iPhone - for timerstopwatch? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744081633a2530355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论