I am working on jQuery and I need to detect when the mouse is static for more than 2 sec for example and then detect if it moves again. I maybe don't have the good name for that so I haven't found anything on google. Thanks !
Asked
Active
Viewed 228 times
-3
-
1Please provide some evidence that you researched the topic. [A query](http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly) on SO would have yielded your answer. – kgdesouz Jun 12 '13 at 23:59
1 Answers
0
As kgdesouz pointed out, here is a simple script using jQuery that handles mousemove
and keypress
events. If the time expires, the page is reloaded.
<script type="text/javascript">
idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval("timerIncrement()", 60000); // 1 minute
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
})
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 19) { // 20 minutes
window.location.reload();
}
}
</script>