Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.

Commit

Permalink
Better handling for window visibility's effect on sound
Browse files Browse the repository at this point in the history
Use visibilitychange events if available; window.onfocus/onblur if not.
  • Loading branch information
townxelliot committed Oct 22, 2013
1 parent 567a46c commit 4bab1b4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions app/js/hangonman.js
Expand Up @@ -960,13 +960,12 @@ window.addEventListener("DOMContentLoaded", function(event)

window.addEventListener("load", function (event)
{
soundBoard.play("background");

window.onblur = function() {
// handlers to pause/play sound when window is hidden/visible
var appHiddenCb = function () {
soundBoard.pause();
};

window.onfocus = function() {
var appVisibleCb = function () {
soundBoard.play("background");

if (isDialogUp)
Expand All @@ -975,6 +974,22 @@ window.addEventListener("load", function (event)
}
};

if ("onvisibilitychange" in window) {
window.addEventListener("visibilitychange", function () {
if (window.hidden) {
appHiddenCb();
}
else {
appVisibleCb();
}
});
}
else {
window.onblur = appHiddenCb;
window.onfocus = appVisibleCb;
}

soundBoard.play("background");
scaleBody(document.body, 720);
}, false);

Expand Down

0 comments on commit 4bab1b4

Please sign in to comment.