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

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

问题描述

如何自动刷新网页,如果已经出现在页面上没有任何活动的给定时间内?

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

推荐答案

如果你想刷新页面,如果没有活动,那么你需要弄清楚如何定义的活动。比方说,我们刷新页面每分钟,除非有人presses一个键或移动鼠标。它使用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天全站免登陆