Created
March 11, 2019 22:10
-
-
Save BenFausch/e29ec5cb088437330e425dd737635cc0 to your computer and use it in GitHub Desktop.
detect inactive tabs with js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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