1.使用visibilitychange
浏览器
标签页被隐藏或显示的时候会触发visibilitychange
事件.
document.addEventListener("visibilitychange", function() { console.log(document.visibilityState); if(document.visibilityState == "hidden") { console.log('隐藏'); } else if (document.visibilityState == "visible") { console.log('显示') } });
2.使用onblur 和 onfocus
通过获取失去焦点判断页面是否在最前端
var count = 1; var interval_output = null; function output_number() { document.body.innerHTML += (count + " "); count++; } // window 失去焦点,停止输出 window.onblur = function() { clearInterval(interval_output); }; // window 每次获得焦点 window.onfocus = function() { // 每 1 秒在页面输出一个数 interval_output = setInterval(function() { output_number(); }, 1000); }