【发布时间】:2012-09-06 12:08:28
【问题描述】:
我希望如果我的页面在 5 分钟内没有任何活动,那么页面将被重定向到预定义的页面。
谁能建议我如何使用 JavaScript 或 jQuery 来做到这一点。
【问题讨论】:
-
一个建议:睡一觉(JS 导管中的
timeout)然后重定向……
标签: javascript jquery redirect
我希望如果我的页面在 5 分钟内没有任何活动,那么页面将被重定向到预定义的页面。
谁能建议我如何使用 JavaScript 或 jQuery 来做到这一点。
【问题讨论】:
timeout)然后重定向……
标签: javascript jquery redirect
如果您想要一个真正的不活动控件,请使用这个库(它也可以控制 鼠标 和 键盘 活动)
jQuery 空闲计时器
http://paulirish.com/2009/jquery-idletimer-plugin/
这是一个使用它的示例:
// 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');
【讨论】:
可能有类似的东西?
<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>
【讨论】:
setTimeout 中使用字符串? :(
您好,请查看这篇关于检测网站中的空闲用户的博客文章。这是一个jQuery Idletimer Plugin。你可以找到demo page here。
简单用法:
// idleTimer() takes an optional argument that defines the idle timeout
// timeout is in milliseconds; defaults to 30000
$.idleTimer(10000);
您从github 获得jQuery Idletimer Plugin 的源代码
【讨论】:
您不能将 META Refresh 设置为您希望超时的秒数并指向相应的重定向页面吗?
<META HTTP-EQUIV="Refresh" CONTENT="60;url=http://www.mydomain.com/redirect_page.html">
我知道这通常不受欢迎,但它避免了对 jQuery(或 Javascript,就此而言)的需求,并且所有浏览器都支持它。
有时简单更好,即使它很丑。
【讨论】: