Problem

You want to react, when the user switches the browser-tab or shifts his focus away from the browser to another app.
This especially interesting on a mobile device and could be used in games to pause the game (stop playing sounds) until the user comes back and continues to play.

Solution

The Page Visibility API will trigger events whenever a user does an action that hides the page away from the screen.

Discussion

See an advanced example of the Page Visibility API in the MDN docs.


More Information