Alex Rivera | Logout

javascript: pause setTimeout();

Asked 2010-10-19T14:36:20.980
145

If I have an active timeout running that was set through

var t = setTimeout("dosomething()", 5000)

Is there anyway to pause and resume it?

Is there any way to get the time remaining on the current timeout?

or do I have to in a variable, when the timeout is set, store the current time, then we we pause, get the difference between now and then?

Edit
Report

1 Answer

1

If you're using jquery anyhow, check out the $.doTimeout plugin. This thing is a huge improvement over setTimeout, including letting you keep track of your time-outs with a single string id that you specify and that doesn't change every time you set it, and implement easy canceling, polling loops & debouncing, and more. One of my most-used jquery plugins.

Unfortunately, it doesn't support pause/resume out of the box. For this, you would need to wrap or extend $.doTimeout, presumably similarly to the accepted answer.

answered 2012-04-17T22:41:29.217

Your Answer