Possible Duplicate:
How To Alter This Code So That It Only Redirects If There Is No Mouse Movement
I want to refresh a web page if there is no activity by the user using Javascript. User activity as in Key Press or mouse click.
Here a basic example
(function(seconds) {
var refresh,
intvrefresh = function() {
clearInterval(refresh);
refresh = setTimeout(function() {
location.href = location.href;
}, seconds * 1000);
};
$(document).on('keypress click', function() { intvrefresh() });
intvrefresh();
}(15)); // define here seconds
This will refresh the page every 15 seconds without a keypress or a click event (but if you have same events defined elsewhere making a stopPropagation()
this won't properly work because the event won't be able to reach the element)
Create a timer (setTimeout
) that will refresh the page, and every time there's a key press or mouse press, just restart the timer.
See this question for code that does most of what you want.
FWIW, here's F.Calderan's answer rewritten to:
--
function setIdle(cb, seconds) {
var timer;
var interval = seconds * 1000;
function refresh() {
clearInterval(timer);
timer = setTimeout(cb, interval);
};
$(document).on('keypress click', refresh);
refresh();
}
setIdle(function() {
location.href = location.href;
}, 15);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With