Reputation: 81
I want that if there is no activity on my page for 5 mins then the page will be redirected to a predefined page.
Can anyone suggest me how to do this using JavaScript or jQuery.
Upvotes: 1
Views: 12067
Reputation: 68616
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>
Upvotes: 6
Reputation: 111
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 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.
Sometimes simple is better, even if it is ugly.
Upvotes: -3
Reputation: 1433
If you want a real inactivity control please use this library (which control mouse and keyboard activity too)
The jQuery idletimer
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');
Upvotes: 8
Reputation: 7249
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.
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 github for jQuery Idletimer Plugin
Upvotes: 0