Best Way to Sleep in Javascript
I am designing a page that will be making xmlHttp to update certain sections of the page. They only need to be updated every couple minutes, so what would be the best way to time it in javascript?
I am designing a page that will be making xmlHttp to update certain sections of the page. They only need to be updated every couple minutes, so what would be the best way to time it in javascript?
decibel.places posted this at 21:59 — 7th February 2009.
He has: 1,494 posts
Joined: Jun 2008
what about setTimeout() or setInterval() ?
teammatt3 posted this at 22:00 — 7th February 2009.
He has: 2,102 posts
Joined: Sep 2003
The best way? Well, you really only have two options:
setTimeout ( expression, timeout );
That will execute expression after timeout, one time.
setInterval ( expression, interval );
and clearInterval()
That will execute expression every interval forever. setInterval() returns an ID which you need to pass to clearInterval() to stop the execution.
decibel.places posted this at 22:13 — 7th February 2009.
He has: 1,494 posts
Joined: Jun 2008
[edited for completeness]
var mytimer = setTimeout ( "expression", timeout-in-milliseconds );
var myinterval = setInterval ( "expression", interval-in-milliseconds );
cancel with
clearTimout(mytimer) or clearInterval(myinterval)
you can also change the value of mytimer or myinterval before they run out
if you do not need to manipulate the timer or cancel it, you do not need the timerId (var mytimer or var myinterval) - however if you do not use an id variable and you call the function before the existing object has triggered, you will create a NEW instance of the timeout or interval with multiple events happening
you can also have a function that calls the one containing setTimeout (it can even be the same function) recursively
here's more
pr0gr4mm3r posted this at 01:27 — 8th February 2009.
He has: 1,502 posts
Joined: Sep 2006
Yup, I found that elated article shortly after posting the question. I had a problem with it at first because IE caches AJAX requests to the same URL, so I was thinking that it wasn't making the call repeatedly. I got around IE's caching problem by sending a post request with the current timestamp.
decibel.places posted this at 01:35 — 8th February 2009.
He has: 1,494 posts
Joined: Jun 2008
an elated article?
I am elated that you enjoyed the article...
pr0gr4mm3r posted this at 02:35 — 8th February 2009.
He has: 1,502 posts
Joined: Sep 2006
You linked to it. Is your cold affecting your memory?
decibel.places posted this at 02:54 — 8th February 2009.
He has: 1,494 posts
Joined: Jun 2008
HaH! - I did not read the url or header - I found it on Google - just glanced over the code...
My apologies
and the cold is affecting me, a bit, but that Advil Cold & Sinus is also making me speedy now, who needs meth? (actually, I knew a guy in college who brought meth from DC up to our Ivy League campus - it was great for studying and stuff, but it kind of ruined my friend's brain...)
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.