Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

JavaScript halts in inactive android Chrome tab

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.

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.

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 changing setInterval/setTimeout to 1000ms is expected and accounted for, but pausing execution completely makes my whole page stop working.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
by (100 points)
Stuff like that needs to be optional. I have a stock ticker going in a browser that needs to be kept updated all the time. I have a workstation with far and away enough resources.
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...