Home  >  Article  >  Web Front-end  >  在移动端上,JS如何能知道浏览器已经被切换到后台了?

在移动端上,JS如何能知道浏览器已经被切换到后台了?

WBOY
WBOYOriginal
2016-06-07 08:43:273474browse

回复内容:

iOS 上利用后端 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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn