Heim > Artikel > Web-Frontend > 在移动端上,JS如何能知道浏览器已经被切换到后台了?
<code class="language-text">var lastFired = new Date().getTime();
setInterval(function() {
now = new Date().getTime();
if(now - lastFired > 5000) { // if it's been more than 5 seconds
alert("onfocus");
}
lastFired = now;
}, 500);
</code>