如何在不活动5分钟后更改页面位置

roh*_*hit 1 javascript jquery redirect

我希望如果我的页面上没有活动5分钟,那么页面将被重定向到预定义的页面.

谁能建议我如何使用JavaScript或jQuery做到这一点.

Has*_*san 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');
Run Code Online (Sandbox Code Playgroud)


dsg*_*fin 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>
Run Code Online (Sandbox Code Playgroud)

  • 为什么你们总是在`setTimeout`中使用字符串?:( (6认同)