javascript 闲置 5 分钟后如何更改页面位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12299783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change a page location after inactivity of 5 mins
提问by rohitpurohit
I want that if there is no activity on my page for 5 mins then the page will be redirected to a predefined page.
我希望如果我的页面上 5 分钟没有活动,那么页面将被重定向到预定义的页面。
Can anyone suggest me how to do this using JavaScript or jQuery.
谁能建议我如何使用 JavaScript 或 jQuery 做到这一点。
回答by Hassan
If you want a real inactivity control please use this library (which control mouseand keyboardactivity too)
如果你想要一个真正的不活动控制,请使用这个库(它也控制鼠标和键盘活动)
The jQuery idletimer
jQuery 空闲计时器
http://paulirish.com/2009/jquery-idletimer-plugin/
http://paulirish.com/2009/jquery-idletimer-plugin/
Here is an example use of it:
这是它的一个使用示例:
// idleTimer() takes an optional argument that defines the idle timeout
// timeout is in milliseconds; defaults to 30000
$.idleTimer(10000);
$(document).bind("idle.idleTimer", function(){
// function you want to fire when the user goes idle
});
$(document).bind("active.idleTimer", function(){
// function you want to fire when the user becomes active again
});
// pass the string 'destroy' to stop the timer
$.idleTimer('destroy');
回答by dsgriffin
Something similar to this maybe?
可能与此类似的东西?
<body onmousemove="canceltimer()" onclick="canceltimer()">
<script type="text/javascript">
var tim = 0;
function reload() {
tim = setTimeout("location.reload(true);",300000); // 5 minutes
}
function canceltimer() {
window.clearTimeout(tim); // cancel the timer on each mousemove/click
reload(); // and restart it
}
</script>
回答by Harsh Baid
Hi please check this blog article regarding detecting idle users in web sites. It is an jQuery Idletimer Plugin. And you can find the demo page here.
您好,请查看这篇关于检测网站空闲用户的博客文章。它是一个jQuery 空闲计时器插件。您可以在此处找到演示页面。
Simple usage:
简单用法:
// idleTimer() takes an optional argument that defines the idle timeout
// timeout is in milliseconds; defaults to 30000
$.idleTimer(10000);
And you get the source from githubfor jQuery Idletimer Plugin
您可以从github获得jQuery Idletimer 插件的源代码
回答by Jason Huebel
Couldn't you just set the META Refresh to the number of seconds you want for the timeout and point to the appropriate redirect page?
难道您不能将 META Refresh 设置为您想要的超时秒数并指向适当的重定向页面吗?
<META HTTP-EQUIV="Refresh" CONTENT="60;url=http://www.mydomain.com/redirect_page.html">
I know this is generally frowned upon, but it avoids the need for jQuery (or Javascript, for that matter) and all browsers honor it.
我知道这通常是不受欢迎的,但它避免了对 jQuery(或 Javascript,就此而言)的需要,并且所有浏览器都尊重它。
Sometimes simple is better, even if it is ugly.
有时简单更好,即使它很丑陋。