Skip to content

Instantly share code, notes, and snippets.

@gordonnl
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordonnl/fc8892cbd7231344ea5e to your computer and use it in GitHub Desktop.
Save gordonnl/fc8892cbd7231344ea5e to your computer and use it in GitHub Desktop.
Pause Inactive Tab Using visibilitychange
var forced = false;
var pauseWhileInactive = function() {
switch (document.visibilityState) {
case 'hidden' :
if (!sfx.volume.value) return;
forced = sfx.volume.value;
sfx.volume.value = 0;
break;
case 'visible' :
if (!forced) return;
sfx.volume.value = forced;
forced = false;
break;
}
};
document.addEventListener('visibilitychange', pauseWhileInactive);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment