2020-09-29 02:04:09 +08:00
|
|
|
(doc => {
|
|
|
|
let visible = "visible",
|
2021-02-12 19:00:06 +08:00
|
|
|
status = visible,
|
|
|
|
timer = 0,
|
2020-08-24 17:26:19 +08:00
|
|
|
wakeUp = () => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
if (status !== visible) {
|
|
|
|
status = visible;
|
|
|
|
}
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
if (status === visible) {
|
|
|
|
status = "idle";
|
|
|
|
dispatchEvent(new CustomEvent("idle"));
|
|
|
|
}
|
|
|
|
}, 10000);
|
2020-09-29 02:04:09 +08:00
|
|
|
},
|
2020-08-24 17:26:19 +08:00
|
|
|
init = () => {
|
|
|
|
init = ()=>{};
|
2020-11-23 23:44:34 +08:00
|
|
|
// Safari
|
2020-11-24 00:05:13 +08:00
|
|
|
addEventListener('pagehide', () => status = "hidden");
|
2020-11-23 23:44:34 +08:00
|
|
|
// Else
|
2020-08-24 17:26:19 +08:00
|
|
|
doc.addEventListener("visibilitychange", () => {
|
|
|
|
status = doc.visibilityState;
|
|
|
|
doc.hidden || wakeUp();
|
2020-09-29 02:04:09 +08:00
|
|
|
});
|
2020-08-24 17:26:19 +08:00
|
|
|
wakeUp();
|
2020-09-29 02:04:09 +08:00
|
|
|
["mousemove","keyup","touchstart"].forEach(t => doc.addEventListener(t, wakeUp));
|
2020-11-23 23:44:34 +08:00
|
|
|
["scroll","pageshow"].forEach(t => addEventListener(t, wakeUp));
|
2020-08-24 17:26:19 +08:00
|
|
|
};
|
2020-08-14 03:25:42 +08:00
|
|
|
|
2020-09-29 02:04:09 +08:00
|
|
|
this.ifvisible = {
|
2020-08-24 17:26:19 +08:00
|
|
|
idle: callback => {
|
|
|
|
init();
|
|
|
|
addEventListener("idle", callback);
|
|
|
|
},
|
|
|
|
now: () => {
|
|
|
|
init();
|
|
|
|
return status === visible;
|
|
|
|
}
|
|
|
|
};
|
2020-09-29 02:04:09 +08:00
|
|
|
})(document);
|