如何在给定的非活动时间后自动重新加载页面 [英] How to automatically reload a page after a given period of inactivity

查看:21
本文介绍了如何在给定的非活动时间后自动重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I automatically reload a webpage, if there have been no activity on the page for a given period of time?

推荐答案

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

If you want to refresh the page if there is no activity then you need to figure out how to define activity. Let's say we refresh the page every minute unless someone presses a key or moves the mouse. This uses jQuery for event binding:

<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>

这篇关于如何在给定的非活动时间后自动重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆