如何在给定的不活动时间后自动重新加载页面

IT技术 javascript ajax
2021-01-28 12:00:22

如果在给定的时间段内页面上没有任何活动,我如何自动重新加载网页?

6个回答

这可以在没有 javascript 的情况下完成,使用这个元标记:

<meta http-equiv="refresh" content="5" >

其中 content ="5" 是页面等待刷新的秒数。

但是你说只有没有活动,那是什么活动?

无活动意味着最终用户不在桌面或浏览其他网站。我所指的站点上没有鼠标/KB 活动。
2021-03-14 12:00:22
我们可以使用帖子变量进行自动刷新吗?
2021-03-24 12:00:22
这不是在回答问题。如果有活动,它无论如何都会重新加载
2021-03-26 12:00:22
尽管这不是答案,因为它没有捕获活动,但仍被赞成,但是当简单地寻找 javascript 刷新时,这个问题位于谷歌搜索结果的顶部。因此,如果您只是希望在设定的时间间隔内自动刷新页面,那么这就是您要走的路。
2021-04-02 12:00:22
很好的答案,认为这必须完成setInterval,很高兴知道这存在!
2021-04-09 12:00:22

如果你想在没有活动的情况下刷新页面,那么你需要弄清楚如何定义活动。假设我们每分钟刷新一次页面,除非有人按下某个键或移动鼠标。这使用 jQuery 进行事件绑定:

<script>
     var time = new Date().getTime();
     $(document.body).bind("mousemove keypress", function(e) {
         time = new Date().getTime();
     });

     function refresh() {
         if(new Date().getTime() - time >= 60000) 
             window.location.reload(true);
         else 
             setTimeout(refresh, 10000);
     }

     setTimeout(refresh, 10000);
</script>
间隔低于不活动时间的原因是您希望以比实际不活动时间高得多的频率检查不活动时间。例如不活动时间为1分钟,间隔为1分钟,如果用户在1秒后移动鼠标然后停止,则仅在2分钟后刷新。间隔越小,刷新时间就越准确。
2021-03-29 12:00:22
如果使用 60000 计算,为什么要将间隔设置为 10000?至少 5 回合它会是假的?
2021-04-01 12:00:22

我还构建了一个不需要 jquery 的完整 javascript 解决方案。也许可以把它变成一个插件。我用它来进行流畅的自动刷新,但看起来它可以在这里帮助你。

JSFiddle 自动刷新

// Refresh Rate is how often you want to refresh the page 
// bassed off the user inactivity. 
var refresh_rate = 200; //<-- In seconds, change to your needs
var last_user_action = 0;
var has_focus = false;
var lost_focus_count = 0;
// If the user loses focus on the browser to many times 
// we want to refresh anyway even if they are typing. 
// This is so we don't get the browser locked into 
// a state where the refresh never happens.    
var focus_margin = 10; 

// Reset the Timer on users last action
function reset() {
    last_user_action = 0;
    console.log("Reset");
}

function windowHasFocus() {
    has_focus = true;
}

function windowLostFocus() {
    has_focus = false;
    lost_focus_count++;
    console.log(lost_focus_count + " <~ Lost Focus");
}

// Count Down that executes ever second
setInterval(function () {
    last_user_action++;
    refreshCheck();
}, 1000);

// The code that checks if the window needs to reload
function refreshCheck() {
    var focus = window.onfocus;
    if ((last_user_action >= refresh_rate && !has_focus && document.readyState == "complete") || lost_focus_count > focus_margin) {
        window.location.reload(); // If this is called no reset is needed
        reset(); // We want to reset just to make sure the location reload is not called.
    }

}
window.addEventListener("focus", windowHasFocus, false);
window.addEventListener("blur", windowLostFocus, false);
window.addEventListener("click", reset, false);
window.addEventListener("mousemove", reset, false);
window.addEventListener("keypress", reset, false);
window.addEventListener("scroll", reset, false);
document.addEventListener("touchMove", reset, false);
document.addEventListener("touchEnd", reset, false);
* 很好/非常感谢 * 这是否用于检测触摸事件?
2021-03-16 12:00:22
这很棒。希望你在这里得到更多的支持。不使用 JQuery 可以获得主要的奖励积分。
2021-03-19 12:00:22
如果我想暂停不重置的计时器,请您提供解决方案吗?
2021-03-22 12:00:22
英雄!这是完美的感谢。我的 PHP 会话设置为一小时后过期,而刷新时间设置为一个多小时。我认为这应该在我追求的不活动功能后完成注销。
2021-03-29 12:00:22
嗯,你知道我不确定。当我创建它时,我对 iPhone 或 iPad 没有太多经验。
2021-04-03 12:00:22
<script type="text/javascript">
  var timeout = setTimeout("location.reload(true);",600000);
  function resetTimeout() {
    clearTimeout(timeout);
    timeout = setTimeout("location.reload(true);",600000);
  }
</script>

除非调用 resetTimeout(),否则上面将每 10 分钟刷新一次页面。例如:

<a href="javascript:;" onclick="resetTimeout();">clicky</a>
隐含的 eval 是纯粹的邪恶!
2021-03-18 12:00:22

基于arturnt的公认答案。这是一个稍微优化的版本,但本质上是一样的:

var time = new Date().getTime();
$(document.body).bind("mousemove keypress", function () {
    time = new Date().getTime();
});

setInterval(function() {
    if (new Date().getTime() - time >= 60000) {
        window.location.reload(true);
    }
}, 1000);

唯一的区别是这个版本使用了setInterval而不是setTimeout,这使得代码更加紧凑。

间隔为 1.000,因为它每秒检查鼠标是否已移动。然后使用 60.000 来确定最后一次鼠标移动是否至少在一分钟前发生。
2021-03-26 12:00:22
1000如果使用 计算,为什么要将间隔设置为60000
2021-04-04 12:00:22