I want to call a js function when there is no activity from user on the web page for specified amount of time. If there is activity from user then reset timeout. I tried to
Most JavaScript events bubble, so you could do something like the following:
click, mousemove, keydown, etc.)document (or maybe document.body for some of them; I can't remember if that's an issue or not).clearTimeout/setTimeoutSo you'd end up with something like this:
var events = ['click', 'mousemove', 'keydown'],
i = events.length,
timer,
delay = 10000,
logout = function () {
// do whatever it is you want to do
// after a period of inactivity
},
reset = function () {
clearTimeout(timer);
timer = setTimeout(logout, 10000);
};
while (i) {
i -= 1;
document.addEventListener(events[i], reset, false);
}
reset();
Note that there are some issues you'd have to work out with the above code:
addEventListener, so it won't work in IE6-8It's more to give you an idea of what you could do.
And now there are four other answers, but I've already typed it all up, so there :P