Blog Has Moved
Tuesday, July 28, 2009
Since migrating my site to Drupal I now no longer use Blogger and hence my blog has moved here.
Since migrating my site to Drupal I now no longer use Blogger and hence my blog has moved here.
Today I was working on a project and at one point I thought it would be useful to clear all the javascript timeOuts on a page. I did a quick google search and did not find anything except this fellow at Experts Exchange who asked the same question. Well if you are like me and do not have a membership to Experts Exchange, then you cannot read the answer. So I spent a few minutes on the problem and came up with a function that clears all the timeouts on a page, even though I ended up not needing it. So I decided to post it here on my blog in case someone else out there has the same conundrum and also does not have a membership to Experts Exchange. So Here it is:
/*first declare an array
to store all the timeOuts*/
var timeOuts= new Array();
/*then where you would normally set
a timeOut, do it like this:*/
timeOuts["a unique name"] = setTimeout('yourDelayedFunction("aVariable")',250);
/*If you want to use a variable (like an
object's id) or pass a variable to your
delayed function you can do it like this:*/
var thisId = "foo";
timeOuts[thisId] = setTimeout('yourDelayedFunction("'+thisId+'")',250);
/*So far, all we have done is store all of our
setTimeouts in an array. Now to clear them all,
just call this simple function:*/
function clearAllTimeouts(){
for(key in timeOuts ){
clearTimeout(timeOuts[key]);
}
}
/*If you want to clear just one of the
timeOuts you can do it like this:*/
clearTimeout(timeOuts["a unique name"]);
One more thing to note is the 250 is the delay in milliseconds, so 250 would be a quarter of a second. So there it is, I hope it is useful to somebody out there.Labels: javascript
14 Comments:
Your code solved a similar problem I had. Nice work... and thanks for posting it.
Exactly what I need. Thanks.
Perfect! Thanks for saving me the time!
I did a similar code, the problem was I didn't know how many times was used the setTimeout, so I did this:
timeOuts = new Array();
var pos=0;
function clearAllTimeouts(){
for(key in timeOuts ){
clearTimeout(timeOuts[key]);
}
}
and to do the setTimeout:
timeOuts[pos] = setTimeout("function()",2000);
pos++;
That certainly works rage.py Of course you can always see how many timeouts have been added to the array by checking the timeOuts.length() attribute as well.
On a related note, you can read EE replies if you find the page as a google result, view the cached copy, and then go to "plain text only". :)
To read EE posts, just keep scrolling down until you hit paydirt.
Is a delight to see your work.
Thanks for sharing your knowledge with us.
Thanks for writing this.
Thanks for posting this
This post has been removed by the author.
Nice....I did something similar to this on my page and I was looking for a better solution but it looks like we both found it.
Good job my man.
Zaf
Oh for rage.py you just have to get the length of the array and clear each entry down the line with a for loop rather than using a custom index.
Good job homie.
I use:-
timeouts[timeouts.length] = setTimeout('foo()', 30);
Then:-
function clear_all_timeouts()
{
if (timeouts) for (var i in timeouts) if (timeouts[i]) clearTimeout(timeouts[i]);
timeouts = [];
}
Post a Comment
<< Home