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

Uma*_*dil 141 javascript ajax

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

amo*_*era 212

这可以在没有javascript的情况下完成,使用此metatag:

<meta http-equiv="refresh" content="5" >
Run Code Online (Sandbox Code Playgroud)

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

但是你说只有在没有活动的情况下才能进行什么样的活动?

  • 虽然这不是答案因为它没有捕获活动,但是这个问题在Google搜索结果的顶部时只是寻找javascript刷新.因此,如果您只是想让页面在设定的时间间隔内自动刷新,那么这就是您的选择. (11认同)
  • 没有活动意味着最终用户不在桌面上或浏览其他网站.我所指的网站上没有鼠标/ KB活动. (2认同)
  • 很好的答案,认为这必须用`setInterval`完成,很高兴知道这存在! (2认同)
  • 这不是在回答问题。如果有活动,它无论如何都会重新加载 (2认同)

Art*_*Art 208

如果您想在没有活动的情况下刷新页面,那么您需要弄清楚如何定义活动.假设我们每分钟刷新一次页面,除非有人按下某个键或移动鼠标.这使用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>
Run Code Online (Sandbox Code Playgroud)

  • 如果使用60000进行计算,为什么要将间隔设置为10000?至少5回合会是假的吗? (5认同)
  • 间隔低于不活动时间的原因是您希望以比实际不活动时间更高的频率检查不活动时间。例如,如果不活动时间为 1 分钟,间隔为 1 分钟,如果用户在 1 秒后移动鼠标然后停止,则刷新只会在 2 分钟后发生。间隔越小,刷新时间就越准确。 (2认同)

bra*_*-it 35

我已经构建了一个完整的JavaScript解决方案,不需要jquery.可能会把它变成一个插件.我用它来进行液体自动清新,但看起来它可以帮助你.

JSFiddle AutoRefresh

// 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);
Run Code Online (Sandbox Code Playgroud)

  • 这很棒。希望您在这里获得更多支持。不使用JQuery可获得主要的奖励积分。 (2认同)

Yur*_*ich 24

<script type="text/javascript">
  var timeout = setTimeout("location.reload(true);",600000);
  function resetTimeout() {
    clearTimeout(timeout);
    timeout = setTimeout("location.reload(true);",600000);
  }
</script>
Run Code Online (Sandbox Code Playgroud)

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

<a href="javascript:;" onclick="resetTimeout();">clicky</a>
Run Code Online (Sandbox Code Playgroud)

  • 隐含的评价是纯粹的邪恶! (2认同)

Han*_*fer 7

基于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);
Run Code Online (Sandbox Code Playgroud)

唯一的区别是此版本使用setInterval代替setTimeout,这使代码更加紧凑。

  • 间隔为1.000,因为它每秒检查一次鼠标是否被移动。然后使用60.000来确定最后一次鼠标移动是否至少在一分钟前发生了。 (3认同)

mik*_*_x_ 5

var bd = document.getElementsByTagName('body')[0];
var time = new Date().getTime();

bd.onmousemove = goLoad;
function goLoad() {
if(new Date().getTime() - time >= 1200000) {
    time = new Date().getTime();
    window.location.reload(true);
    }else{
        time = new Date().getTime();
    }
}
Run Code Online (Sandbox Code Playgroud)

每次移动鼠标时,它都会检查您上次移动鼠标的时间.如果时间间隔大于20',它将重新加载页面,否则它将更新最后一次移动的鼠标.