Jump to content

Pausing a Javascript game on incoming call


Recommended Posts

If running in a browser, it will most probably lose focus when a call comes.


game.onPause.add() will add a callback for when you do pause your game, but won't initiate the pause itself.


As for the focus, try something like this:

window.onblur = function() {    console.log("lost focus");    // Pause}
Link to comment
Share on other sites

Thanks. Tried this with no success ;(

I receive the window.onblur event in my client when pressing the home button in my iPhone, but not when a call is coming in..

It might be that the browser doesn't lose focus, or is there another event worth trying?

Link to comment
Share on other sites

Sure. I gathered the relevant parts:

This is put in the create: game.onPause.add(pauseGame, game);

pauseGame() - is a global function that does some things and sets - game.paused = true;


The onPause is triggered when I minimize the browser on my phone, move tab on my desktop and some other cases - so I managed to get the pauseGame() function working in these case.

The thing is that game.onPause.add(...) is not triggered on receiving the call. I also tried capturing the following events when call is received, using addEventListener() - onpageshow, onpagehide, onblur, onfocus, onfocusin, onfocusout, and more..


no success ;(


Thank you for the interest and help!!

Link to comment
Share on other sites

So the update call is still happening while on a phone call?


Maybe you can put something in the render function of your game loop. You will need to test if the render function is also being called during the phone call. If the render isn't called for about 5 updates then you're not the active app. 

function update(){    ticks++;    if(ticks > 5){        pause();    }}function render(){    ticks = 0;}

I haven't tested this, just throwing ideas out there ;)

Link to comment
Share on other sites

Have you tried the Page Visibility API? More specifically, listening for the "visibilitychange" event?

//startSimulation and pauseSimulation defined elsewherefunction handleVisibilityChange() {  if (document.hidden) {    pauseSimulation();  } else  {    startSimulation();  }}document.addEventListener("visibilitychange", handleVisibilityChange, false);


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.

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.


  • Recently Browsing   0 members

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