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