14

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?

4
  • Hey. Did you find any solution for the problem?, I am also facing the same problem Commented Jan 28, 2016 at 8:32
  • As you can see from the answer below, the only solution is to keep the screen on and the browser window open thereby keeping the tab active.
    – Benjam
    Commented Jan 29, 2016 at 20:52
  • You can try bg-timer-helper. I'm not sure how it works for all execution types, but it works for setTimeout/setInterval Commented Oct 16, 2018 at 0:17
  • Have (any) browser vendors documented any info about this "optimisation" for mobile devices? I've have a system which requires the "opener" page to service ajax requests for the active (opened) page but not only is the opener page not sending the requests, but the active tab isn't even getting notifications of an ajax timeout error (except under the unlikely condition that the user "tabs back" to the opener page). I need to know if an iframe on an active tab or a frameset under the active tab stays active or not. Commented Jan 4, 2021 at 13:28

1 Answer 1

12

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.

4
  • 2
    Is there a config setting somewhere that I can change to bypass this restriction? Maybe a combination of JavaScript setting and browser setting that will allow processing for a longer period of time?
    – Benjam
    Commented Feb 28, 2014 at 17:04
  • 1
    No. Sorry. There are projects such as Service Worker which might in the future let some tasks stay awake in the background, but that is a little way away.
    – Kinlan
    Commented Feb 28, 2014 at 20:02
  • @Kinlan Thanks for your answer, but are there any updates which can help in resolving? Commented Jan 28, 2016 at 8:00
  • 2
    What if I have my phone plugged into a charger? It seems I should be allowed to override this. Respecting vs Restricting me as the user is a different thing entirely.
    – nu everest
    Commented Nov 13, 2017 at 4:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.