Jump to content

How to prevent javascript pause when not focused


C0PT3R
 Share

Recommended Posts

Hi, first of all, please excuse my english as it may not be perfect. I'm currently working on an online multiplayer game (webGL -> PIXI, webRTC -> PeerJS) and I just realized that when a player focuses out of the browser tab that displays the game, it stops updating. Searching the web, I found many forum posts talking about visibility API, but none would explain how to achieve my goal. Also heard about Phaser's disableVisibilityChange (or something like that). If Phaser can do it, it means there's a way. Digging through its code doesn't help, as I can't figure out how it is working... Does anybody have a clue?

 

For information, I use requestAnimationFrame.

Link to comment
Share on other sites

Browsers can (and often will) stop running RAF callbacks if the tab isn't visible. There isn't a way to stop this and it's up to the browser to determine what 'visible' means, and how long it should wait because pausing RAF. To get around it you can use the Page Visibility API to tell when the page is 'hidden' and then act accordingly (either stopping network updates, or starting a setTimeout loop, which doesn't stop based on visibility)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...