0

I have this code which works ok but I would like to stop polling and clearInterval if the user is inactive (no mouse move) after say 5 iterations rather than be in a continuous loop.

var i, active = new Date, iter = 1;

$(window).on('mousemove', function(e){
active = new Date;          
});

i = setInterval(function(){
    if(new Date().getTime() - active.getTime() < 1000){
     console.log("sending active event to server.....");
             iter = 1;  
    else{
      iter++;
        if(iter >= 5){
           clearInterval(i);
        }
     }          
}, 2000);   

right now it checks every two seconds in this example. I would like to check the active date say 5 times and if its expired 5 iterations in a row, call clearInterval(i)... so something inside the mousemove callback should reinitialize the setInterval only if it's currently not running. How can I accomplish this? Thanks for any tips and samples. I'd like to keep using an anonymous function if possible.

Charles
  • 50,943
  • 13
  • 104
  • 142
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190

2 Answers2

1

Seperate the Interval function

function intFunc(){
    if(new Date().getTime() - active.getTime() < 1000){
     console.log("sending active event to server.....");
             iter = 1;  
    else{
      iter++;
        if(iter >= 5){
           clearInterval(i);
        }
     }          
};

Now, call them on the two places you need

var i;
$(window).on('mousemove', function(e){
  active = new Date;          
  i = setInterval(intFunc, 2000);

});
i = setInterval(intFunc, 2000);
Starx
  • 77,474
  • 47
  • 185
  • 261
0

One simple way would just be to remove the clearInterval call, and instead only poll the server when iter < 5.

But that's still a little wasteful, as the handler is still being called when it has nothing to do, which is bad when you want your laptop/phone to stay in powersaving mode. So what I'd do is basically what you have, but after calling clearInterval, set up a one-time mousemove handler that restarts polling.

I'm not seeing a way to do that without naming a function (I'm presuming you don't want to get into Y-combinators and the like), but you can hide its name – and the others – from the outside world by using an anonymous function around the whole thing:

(function () {
    var i, active = new Date, iter = 1;

    $(window).on('mousemove', function(e) {
        active = new Date;
    });

    function startPolling() {
        i = setInterval(function() {
            if (new Date().getTime() - active.getTime() < 1000) {
                console.log("sending active event to server.....");
                iter = 1;
            }
            else{
                iter++;
                if (iter >= 5) {
                    clearInterval(i);
                    $(window).one('mousemove', function () {
                        startPolling();
                    });
                }
            }
        }, 2000);
    }

    startPolling();
})();
deltab
  • 2,498
  • 23
  • 28