snappymail/dev/External/ifvisible.js
2020-08-24 11:26:19 +02:00

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;
}
};
})();