0

I need to refresh a page after 10 minutes of inactivity.

What I mean by inactivity : no mouse move and/or no clicks on the body

Here's what I have so far (that piece of code is working fine)

idleTime = 0;

function timerIncrement() {
    idleTime = idleTime + 1;

    if (idleTime > 10) {
    window.location.reload();
    }
}

$j(document).ready( function(e) {
    var idleInterval = setInterval("timerIncrement()", 60000); // 1 min

    $j(this).mousemove(function () {
        idleTime = 0;
    });

    $j(this).click(function () {
        idleTime = 0;
    });
}

What I dont like here, is the fact that its reseting the timer on every single mousemove event. And Im worried about performance.

What I would like is something like : Check every 2 minutes if the mouse position changed. If yes increment timer. Not sure how to do that.

Any help?

Lelly
  • 960
  • 3
  • 15
  • 29

1 Answers1

0

You could add a flag variable (eg: hasMouseMoved), update it on the mousemove handler and remove the event handler, then whenever the timer fires change the code to:

if ((idleTime > 10) || (idleTime%2==0 && hasMouseMoved))
{
      window.location.reload();
}
drhelado
  • 33
  • 6
Andrew Lewis
  • 5,176
  • 1
  • 26
  • 31