tags:

views:

228

answers:

1

I am using a Jquery time delayed fader like below, but need to reinitialize it based upon events in the code, ie knock the timer back to zero. How do I do this? I've tried just calling fader() again.

function fader(){ 

$(ajdiv).delay(10000, function(){$(ajdiv).fadeOut()});

}
jQuery.fn.delay = function(time,func){ //run a delayed function

return this.each(function(){

    setTimeout(func,time);

});

};
+1  A: 

You need to save the timeoutID returned from setTimeout. You can call window.clearTimeout(timeoutID) to remove the timer and stop the event from happening. You'd have to create a new one afterwards in case of "resetting".

The mozilla documentation for setTimeout.

Check out this question too, it's got a (in my opinion) more elegant solution to I think the same problem you're having.

Robert Massa
Thanks! :D Do you know if this works for other browsers also?
Yes, it's supported by all mayor browsers.
Robert Massa