Reputation: 3418
I want my html5 canvas game to automatically pause when the user opens up another tab inside the browser. What is the name of the event that is fired when a user does this?
Upvotes: 0
Views: 83
Reputation: 600120
As an alternative to the blur event suggested in another answer, you could use the new window.hidden
property of HTML5.
Good places to read up on this are:
The biggest problem using this specification today is that you'll have to cater for vendor prefixes to handle it in all browsers. But if you ignore that it really comes down to:
if (!window.hidden) {
// do whatever you normally do to render a frame
}
There are corresponding visibilitychange
events in case you'd prefer to keep the detection out of your game loop.
Upvotes: 0
Reputation: 10040
$(window).blur( function() {
});
or in js:
window.onblur = function() {
}
There is no "new tab opened" event. But pausing on the blur event will do what you want - whenever the window loses focus the game will pause. You can then resume game in the focus event.
Upvotes: 2