How to pause a setTimeout call?

Possible duplicate:
javascript: pause setTimeout ();

I am using jQuery and working on a notification system for my site. Notifications automatically disappear with the setTimeout function.

How can I stop the setTimeout call timer?

For example, I would like to pause the setTimeout call when the mouse is above the notification, and continue to scroll down the mouse ...

I googled "pause setTimeout" with no luck.

I am currently clearing the setTimeout call with clearTimeout and at the same time the mouse notification fades, but it would be nice to have this pause effect.

Any ideas?

+7
javascript jquery settimeout
source share
3 answers

Try it.

var myTimeOut; $(someElement).mouseout( function () { myTimeOut = setTimeout("mytimeoutfunction()", 5000) }); $(someElement).mouseover( function () { clearTimeout(myTimeOut); }); 
+10
source share

You could not add the PausableTimeout class:

(JS may not be true, but it shouldn't be too hard to get it working):

  function PausableTimeout(func, millisec) { this.func = func; this.stTime = new Date().valueOf(); this.timeout = setTimeout(func, millisec); this.timeLeft = millisec; } function PausableTimer_pause() { clearTimeout(self.timeout); var timeRan = new Date().valueOf()-this.stTime; this.timeLeft -= timeRan; } function PausableTimer_unpause() { this.timeout = setTimeout(this.func, this.timeLeft); this.stTime = new Date().valueOf(); } PausableTimer.prototype.pause = PausableTimer_pause; PausableTimer.prototype.unpause = PausableTimer_unpause; //Usage: myTimer = new PausableTimer(function(){alert("It works!");}, 2000); myTimer.pause(); myTimer.unpause(); 

Of course, it would be nice to add some error checking there (you do not need to be able to disable the timeout several times and end with hundreds of timeouts!), But I will let this be your job: P

+6
source share

Use clearTimeout() for the mouseover event and use setTimeout() again in the mouseout event.

http://www.w3schools.com/jsref/met_win_cleartimeout.asp

+4
source share

All Articles