10

I want to run the following code:

ajaxUpdate(10);

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

1
  • Hope your server can handle the beating. :) Commented Jan 20, 2009 at 15:14

6 Answers 6

39
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,

Sign up to request clarification or add additional context in comments.

4 Comments

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

You can also do it with

setTimeout(function() {ajaxUpdate(10)}, 1000);

3 Comments

this will be one time only, not iterative
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
I find the second answer (from Kent Fredric) way more complete and detailed. It also presents multiple ways to achieve your goal. This answer is good as well, but it's just incomplete and not as useful for the community, so i personally suggest you accept Kent's answer.
5

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);

19 Comments

You should use window.setInterval() instead of only setInterval()
do I need to use var myInterval=... or can I just call the code on the right side of the = and it will work?
You only need "var myInterval = " if you want to stop it again at some point
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.
If called without assignment, will the code continue to run per 1 second until canceled, or will it only run once?
|
1

You can use this JavaScript Timer class.

Comments

0

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

Comments

-1

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 ;)

4 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.