2012-09-06 37 views
1

我希望如果我的页面上没有任何活动持续5分钟,那么页面将被重定向到预定义页面。如何在5分钟不活动后更改页面位置

任何人都可以建议我如何使用JavaScript或jQuery做到这一点。

+0

定义“没有活动” – asawyer

+0

一个建议:做一个睡眠(在JS ductus'超时'),然后重定向... – feeela

回答

6

类似的东西可能吗?

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

为什么你们总是在'setTimeout'中使用字符串? :( –

+1

键盘或类似的东西也需要(我可以使用键盘只) –

8

如果你想有一个真正的活动控制,请使用这个库(控制鼠标键盘活动太)

jQuery的idletimer

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'); 
+0

+1应该被检查为解决方案 – Simon

0

您好,请检查这篇博客文章有关检测网站中的闲置用户。这是一个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); 

,你会得到从githubjQuery Idletimer Plugin

-3

来源不能你刚才设置META刷新您要为超时并指向的秒数适当的重定向页面?

<META HTTP-EQUIV="Refresh" CONTENT="60;url=http://www.mydomain.com/redirect_page.html"> 

我知道这通常是令人难以接受的,但它避免了jQuery的需要(或Javascript,对于这个问题)和所有浏览器都兑现了。

有时候简单会更好,即使它很丑。

+2

这根本不起作用。原始问题要求检测闲置。在这种情况下,它会刷新是否或者用户没有空闲。 – Crisfole

相关问题