Last active
July 27, 2022 07:29
-
-
Save evolify/15b74e8406cf9d78494a05392a4d84e6 to your computer and use it in GitHub Desktop.
visibility change
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
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