mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-11-10 17:13:38 +08:00
42 lines
872 B
JavaScript
42 lines
872 B
JavaScript
(() => {
|
|
const doc = document,
|
|
visible = "visible",
|
|
wakeUp = () => {
|
|
clearTimeout(timer);
|
|
if (status !== visible) {
|
|
status = visible;
|
|
}
|
|
timer = setTimeout(() => {
|
|
if (status === visible) {
|
|
status = "idle";
|
|
dispatchEvent(new CustomEvent("idle"));
|
|
}
|
|
}, 10000);
|
|
};
|
|
|
|
let status = visible,
|
|
timer = false,
|
|
init = () => {
|
|
init = ()=>{};
|
|
doc.addEventListener("visibilitychange", () => {
|
|
status = doc.visibilityState;
|
|
doc.hidden || wakeUp();
|
|
}, false);
|
|
wakeUp();
|
|
doc.addEventListener("mousemove", wakeUp);
|
|
doc.addEventListener("keyup", wakeUp);
|
|
doc.addEventListener("touchstart", wakeUp);
|
|
addEventListener("scroll", wakeUp);
|
|
};
|
|
|
|
window.ifvisible = {
|
|
idle: callback => {
|
|
init();
|
|
addEventListener("idle", callback);
|
|
},
|
|
now: () => {
|
|
init();
|
|
return status === visible;
|
|
}
|
|
};
|
|
})();
|