views:

721

answers:

6

I want to run the following code:

ajaxUpdate(10);

With a delay of 1 second between each iteration. How can I do this?

+4  A: 

You can use setInterval() for that. Create an anonymous function to be called, and use the time in milliseconds:

var myInterval = window.setInterval(function() { ajaxUpdate(10); }, 1000);
Greg
You should use window.setInterval() instead of only setInterval()
Georg
Updated - thanks @gs
Greg
do I need to use var myInterval=... or can I just call the code on the right side of the = and it will work?
Click Upvote
You only need "var myInterval = " if you want to stop it again at some point
Greg
It will work without the assignment, but as Kent Fredric explains in his answer, the assignment gives you the option of canceling the interval at a later time.
Már Örlygsson
If called without assignment, will the code continue to run per 1 second until canceled, or will it only run once?
Click Upvote
It continues to run - if you want it to run once, use setTimeout instead of setInterval
Greg
@Click - setInterval does that for you. It is generally considered very bad practice not to capture the interval ID with the assignment though, even if you don't intend to use it right now.
annakata
I want it to run once and the same line of the code to be called again to have it run again 1 sec later. I think Raibaz's answer below will do this?
Click Upvote
I will have a dynamic # of these timers running so I don't want to store the interval ID for each of them (too complicated to explain)
Click Upvote
@Click - no, raibaz answer (setTimeout) does this one time only, compared to setInterval which does it repeatedly indefinitely.
annakata
you can store all these intervalids in an array for management
annakata
@annakata, the same function (ajaxUpdate) will be setting the line from Raibaz's answer if some conditions meet which mean the update will need to be carried out again. So when the code runs 1 sec later, it will set the same timer again (if needed) and it'll run again.. right?
Click Upvote
@anna, yes but then I have to store which array ID corresponds to which ID in the database etc, i want to keep things as simple as possible
Click Upvote
er, I have no idea why you'd want to be recording timer IDs in a database. They're only relative to the user.
Kent Fredric
The ID of the item they're getting the update for, from the database. Like i said its complicated...
Click Upvote
You're confused, the value returned by "setInterval" is merely a number stating the *timer* instance in respect to all other timers currently in scope of the browser, its merely an array index, If your shipping the result of setInterval to a server, you are almost *certainly* doing something wrong
Kent Fredric
@click - it's more efficient and *far* clearer for you to use set/clear interval where you mean something to iterate than to continually set a new timer. The only exception to this is where the period is changing. There's a reason the setInterval answers are upvoted...
annakata
(cont'd) If on the other hand the typical scenario is that the second timer will not be executed then this is not what I would consider iterative, and setTimeout will be more appropriate.
annakata
Its complicated, but before each iteration i want it to check a condition and only if that is true it should run the timer.
Click Upvote
+13  A: 
var i = window.setInterval( function(){ 
          ajaxUpdate(10); 
 }, 1000 );

This will call ajaxUpdate every second, until such a time it is stopped.

And if you wish to stop it later:

window.clearInterval( i );

If you wish to only run it once however,

var i = window.setTimeout( function(){ 
          ajaxUpdate(10); 
 }, 1000 );

Will do the trick, and if you want to stop it running before it gets around to running once

window.clearTimeout(i);

The "window" prefix is not strictly nessecary, but its a good idea, because you never know when somebody else might like to create something else with the same name in visible scope that behaves differently.

For a complete reference on this, I always find MDC Very Helpful:

Also, you may wish to read this article on timers by John Resig,

Kent Fredric
You should use window.setInterval() instead of only setInterval() (same for clearInterval)
Georg
linking to the javascript docs would make a nice addition: https://developer.mozilla.org/en/DOM/window.setInterval
Már Örlygsson
Why should you prepend it with "window." ?
I.devries
You don't have to, its just a good idea, like I said in my statement.
Kent Fredric
A: 

You can also do it with

setTimeout(function() {ajaxUpdate(10)}, 1000);
Raibaz
this will be one time only, not iterative
annakata
This is the right one for this situation because b4 each iteration i want to check a condition, and only if it is met do i want the timer to be set 4 the next iteration. With setInterval it would run auto. without checking condition which i dont want. And this 1 is more simpler
Click Upvote
A: 

You can use the function setTimeout(String fonc, Integer delay). For example, to execute your code each second you can do :

window.setTimout("ajaxUpate",100);

Hope i answer to your question ;)

damdec
oops, everybody answers in the same minute !
damdec
Using strings is highly not recommended, its too ambiguous.
Kent Fredric
Also, in your example, ajaxUpdate will get called with a semi-random value, not the fixed value '10' he requested.
Kent Fredric
Ok, thanks for the comment, i didn't known it works with a function as first paramter.
damdec
A: 

You can use too jQuery Timers: http://plugins.jquery.com/project/timers

Click Ok
A: 

You can use this JavaScript Timer class.

Ramesh Soni