2

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);

});

};
user132192
  • 49
  • 1
  • 6

2 Answers2

1

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.

Community
  • 1
  • 1
Robert Massa
  • 4,345
  • 1
  • 32
  • 44
1

This is my actual code that I used (it makes a new element slide from left stay for time given to my custom delay function and then disappear again):

$element.next().next().hide().show('slide', {direction : 'left'}, 700, function () {$(this).delay(10000, function() {$element.next().next().hide('slide', {direction: 'left'}, 700)})});
kosta
  • 13
  • 3