Disable Pausing When Tabbing Away

Hi all, apologies if there’s been a recent post about this, but I did a light bit of searching, and found these threads (here and here) which are both at this point years old.

Issue as stands, I’m running a networked game, a fair bit of which is fortunately not using the PlayCanvas update function (which if I understand correctly, is the source of the pausing?).

However, there are parts of it that do use the update function, notably the turn timer right now. Given that it’s important that the turn timer stays synchronous on the client-side with the other client and the server, I’m wondering if I can stop PlayCanvas from pausing when the user tabs away.

If it’s not possible, I suppose I could work around it, but it’s better to know it now than further down the line.

Hi @TingleyWJ,

The main reason Playcanvas is pausing is because the browser will stop by default all requestAnimationFrame/WebGL rendering calls when the tab is inactive. There are plenty of reasons for doing so (conserve resources, save battery etc) and from what I know there isn’t any workaround to keep rendering.

For networked games that require to keep pushing updates/messages out, you usually make use of the browser window.setTimeout/setInterval methods. Those will continue working and looping even when the tab is inactive. Just note that they may fire at a different pace than the one set since the browser allocates less resources to inactive tabs.

Hope I’ve been of help.

1 Like

There’s also Date.now() which can be used to get starting time of a turn https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

You can then use it in update to check how much time has passed since the start time.