-3

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 !

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Louis Bewer
  • 63
  • 1
  • 5
  • 1
    Please 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 Answers1

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>
Community
  • 1
  • 1
eggy
  • 2,836
  • 3
  • 23
  • 37