我正在运行一个 JavaScript 计时器,它在为 iPhone 5 构建的网络应用程序中播放蜂鸣声。当应用程序位于前台时,计时器工作正常。但是,当应用程序移至后台时,计时器不会运行,并且不会播放蜂鸣声。
下面是我的代码。
setInterval( function(){
beepAudio.get(0).play();
}, timeInterval);
beepAudio是我嵌入html页面的音频标签的id。
我也尝试过 setTimeout,但没有帮助。 iOS版本为6.1.4。
当 iPhone 5 的应用程序处于后台时,有没有办法运行计时器?
感谢任何帮助。
最佳答案
来自App States and Multitasking在后台执行和多任务部分(强调由我添加):
In iOS 4 and later, multitasking allows apps to continue running in the background even after the user switches to another app while still preserving battery life as much as possible. Most apps are moved to the suspended state shortly after entering the background. Only apps that provide important services to the user are allowed to continue running for any amount of time.
同一文档,关于暂停状态:
The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code.
所以你想要做的事情似乎是不可能的。
关于当 iPhone 5 应用程序处于后台时,Javascript 计时器不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18534415/