Skip to content

Instantly share code, notes, and snippets.

@BenFausch
Created March 11, 2019 22:10
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 BenFausch/e29ec5cb088437330e425dd737635cc0 to your computer and use it in GitHub Desktop.
Save BenFausch/e29ec5cb088437330e425dd737635cc0 to your computer and use it in GitHub Desktop.
detect inactive tabs with js
//handles tab inactivity
var vis = (function() {
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();
//call the function, use same function as param for verification:
vis(function() {
if (vis()) {
console.log('focus!')
var id = window.setTimeout(function() {}, 0);
while (id--) {
window.clearTimeout(id); // will do nothing if no timeout with id is present
}
jQuery('.messages ul li').removeClass('active')
videoElement.load()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment