Skip to content

Instantly share code, notes, and snippets.

@tsmd
Created November 15, 2021 14:48
Show Gist options
  • Save tsmd/b3bb79f39070c7bfd1c8b970e28b37e5 to your computer and use it in GitHub Desktop.
Save tsmd/b3bb79f39070c7bfd1c8b970e28b37e5 to your computer and use it in GitHub Desktop.
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