JavaScript的停止在非激活的Andr​​oid浏览器选项卡选项卡、浏览器、JavaScript、Andr

2023-09-09 21:08:58 作者:Rude Boy

我有一个小小的网页的流音乐,我可以听我的电话。页面上的JavaScript的设定超时加载下一首歌曲并播放它时,第一首歌就结束了。

I have a tiny webpage that streams music that I can listen to on my phone. The JavaScript on the page sets a timeout that loads the next song and plays it when the first song is over.

当标签处于活动状态,一切工作正常。但在标签变为无效,超时持续几分钟,一旦工作,然后根据当前播放的歌曲完成后,下一首歌曲不加载或播放。

While the tab is active, everything works fine. But after the tab goes inactive, the timeouts continue to work for a few minutes, and then as soon as the currently playing song finishes, the next song does not load or play.

我假定这是由于Chrome浏览器的JavaScript暂停执行非活动选项卡,但我不希望有让我的屏幕上的全部时间我在玩音乐。

I'm assuming this is due to Chrome pausing JavaScript execution for the inactive tab, but I don't want to have to keep my screen on the whole time I'm playing music.

我已经调试使用Chrome移动调试器通过设置超时运行每次只显示当前日期为500ms的code。一旦屏幕变黑,超时下降到每1000毫秒,这是预期的,但几分钟后,在超时完全停止。但是,一旦选项卡再次处于活动状态,所有的暂停超时启动和运行。

I've debugged the code using the Chrome mobile debugger by setting a timeout to run every 500ms that just shows the current Date. Once the screen goes black, the timeout drops to once every 1000ms, which is expected, but after a few minutes, the timeout stops altogether. But once the tab is active again, all the paused timeouts activate and run.

Chrome浏览器改变的setInterval / setTimeout的到1000毫秒,预计占了,但暂停执行彻底让我的整个页面停止工作。

Chrome changing setInterval/setTimeout to 1000ms is expected and accounted for, but pausing execution completely makes my whole page stop working.

我试图努力保持标签激活运行Ajax请求,每隔一段时间,但这些暂停为好。没有什么我已经试过保持运行的JavaScript。

I've tried trying to keep the tab active be running AJAX requests every so often, but those pause as well. Nothing I've tried keeps the JavaScript running.

基本上,有没有解决的办法?有没有一种方法来保持选项卡激活时,屏幕是关闭的?

Basically, is there a way around this? Is there a way to keep the tab active when the screen is off?

推荐答案

没有。对于性能和电池寿命的原因,我们停在标签,当前不活动的所有处理。这些网页上运行的非常约束相比,桌面设备和设备,我们需要尊重用户。

No. For performance and battery life reasons we stop all processing on tabs that are not currently active. The devices that these pages run on are very constrained compared to a desktop device and we need to respect the user.

我们是有选择保留音乐和音频播放的背景(当你的屏幕是关闭的),但你不能移动到下一首曲目就像你所需要的。

We do have the option to keep music and audio playing in the background (when your screen is off) but you can't move to the next track like you need.