Skip to content

Instantly share code, notes, and snippets.

@evolify
Last active July 27, 2022 07:29
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 evolify/15b74e8406cf9d78494a05392a4d84e6 to your computer and use it in GitHub Desktop.
Save evolify/15b74e8406cf9d78494a05392a4d84e6 to your computer and use it in GitHub Desktop.
visibility change
export function onVisibilityChange(cb) {
let hidden;
let visibilityChange;
if (typeof document.hidden !== "undefined") {
// Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
document.addEventListener(visibilityChange, handleVisibilityChange);
function handleVisibilityChange() {
cb(!document[hidden]);
}
}
onVisibilityChange((visible) => {
if (visible) {
// page is visible
} else {
// page is hidden
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment