Best Way to Sleep in Javascript

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

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's picture

He has: 1,494 posts

Joined: Jun 2008

what about setTimeout() or setInterval() ?

teammatt3's picture

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's picture

He has: 1,494 posts

Joined: Jun 2008

teammatt3 wrote:

[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's picture

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's picture

He has: 1,494 posts

Joined: Jun 2008

pr0gr4mm3r wrote:
... that elated article ...

an elated article?


e⋅lat⋅ed [i-ley-tid]
–adjective very happy or proud; jubilant; in high spirits: an elated winner of a contest.

I am elated that you enjoyed the article... Sticking out tongue

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

an elated article?

You linked to it. Is your cold affecting your memory? Laughing out loud

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

pr0gr4mm3r wrote:
You linked to it. Is your cold affecting your memory? :D

HaH! - I did not read the url or header - I found it on Google - just glanced over the code...

My apologies Cry

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.