thorst / jquery-idletimer

provides you a way to monitor user activity with a page.
MIT License
721 stars 144 forks source link

stopping and restarting the timer #19

Closed sam327 closed 10 years ago

sam327 commented 11 years ago

I can get my function to start, and I can get it to stop, but I can't get it to restart again after stopping it.

function idlecheck(){
    $(document).idleTimer( 5000 );
    $(document).bind("idle.idleTimer", function(){
        //functionality to occur when idle
        $('#menu').fadeOut(1000);
    });
    $(document).bind("active.idleTimer", function(){
        //functionality to occur when active again
        $('#menu').fadeIn(1000);
    });
}

// start the timer when the doc is ready.
$(document).ready(function(){
    idlecheck();
});

I set up a button to disable the timer.

$('.disabletimer').click(function(x) {
    $(document).idleTimer("destroy");
}

And then one to start it again.

$('.enabletimer').click(function(x) {
    idlecheck();
}

Once the destroy item has been run, i can't start the idle timer at all.

Funny item is that if I don't have the idlecheck run on load, the "enabletimer" button will let make it start.

Is there a better way to pause or stop/restart the timer?

tysonnero commented 10 years ago

I would like to also know what the best way to pause, stop, and restart the timer.

maximelebreton commented 10 years ago

+1

sam327 commented 10 years ago

So, my goal was to hide a menu when idle and fade it back in when the mouse was moved again. UNLESS that particular menu was open, and then I wanted it to always be shown.

So, instead of ending/restarting the timer, I added an if-check on whether or not the menu was open.

Here's the start of the code:

var idletimeout = 3500

function idlecheck(){
$(document).bind("idle.idleTimer", function(){
     if($("#mymenu").hasClass("collapsed")) {
    } else {    
   $('#containerofmymenu').fadeOut(1000);
    };
});
idlecheck();

There's also an active.idleTimer bit, and then another piece to restart the idletimer when I open and close that window.

Hope that's helpful to someone.

Later in the code, I've got a

It's not quite the solution I was looking for at the time, but it produced the results I wanted.

thorst commented 10 years ago

I believe my branch implements this correctly. https://github.com/thorst/jquery-idletimer/blob/master/src/idle-timer.js

thorst commented 10 years ago

Pause, resume, reset have been added in 1.0.0