Created
November 15, 2021 14:48
-
-
Save tsmd/b3bb79f39070c7bfd1c8b970e28b37e5 to your computer and use it in GitHub Desktop.
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
let pageVisible = true; | |
function updatePageVisibility(e) { | |
if (e.type === "visibilitychange") { | |
pageVisible = !document.hidden; | |
} else { | |
const typeVisibilityMap = { blur: false, focus: true, pagehide: false, pageshow: true }; | |
pageVisible = typeVisibilityMap[e.type]; | |
} | |
} | |
document.addEventListener("visibilitychange", updatePageVisibility); | |
window.addEventListener("focus", updatePageVisibility); | |
window.addEventListener("blur", updatePageVisibility); | |
window.addEventListener("pagehide", updatePageVisibility); | |
window.addEventListener("pageshow", updatePageVisibility); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment