JavaScript:setInterval函数仅在浏览器空闲时才计数?

当浏览器通过setInterval() 函数处于非活动状态时,我想正确注销。当浏览器处于活动状态时,setInterval不会计数。但是,当浏览器保持空闲状态时,该功能正在计数。


即使浏览器处于活动状态,如何实现setInterval计数?


的JavaScript


function session_checking() {

    $.post("activitytime.php");

}   

*//Every after 7 mins (60*7) after browser is inactive run activitytime.php*'   

setInterval(session_checking, 1000*10);

的PHP


<?php

session_start();

//Do the necessary database update

session_destroy();


牧羊人nacy
浏览 222回答 1
1回答

芜湖不芜

您可以使用“窗口”事件和“ clearInterval”var sessionTimer;function startTimer(){&nbsp; &nbsp; &nbsp; sessionTimer = setInterval(session_checking, 1000*10);}function stopTimer(){&nbsp; &nbsp; if(sessionTimer){&nbsp; &nbsp; &nbsp; &nbsp; clearInterval(sessionTimer);&nbsp; &nbsp; }}function session_checking() {&nbsp; &nbsp; &nbsp;$.post("activitytime.php");}&nbsp;//means tab is activewindow.onfocus = function () {&nbsp;&nbsp; &nbsp;stopTimer();&nbsp;};&nbsp;//means tab is passivewindow.onblur = function () {&nbsp;&nbsp; &nbsp; startTimer();};&nbsp;另外,如果您想在窗口关闭时调用API,则可以调用window.onbeforeunload = function(e) {&nbsp; &nbsp; &nbsp; //call api here&nbsp; &nbsp; &nbsp; return true;};
打开App,查看更多内容
随时随地看视频慕课网APP