views:

885

answers:

8

Is there any way to call a function periodically in JavaScript.

+7  A: 

See documentation for setTimeout and setInterval

Ken Browning
+3  A: 

yes - take a look at setInterval and setTimeout for executing code at certain times. setInterval would be the one to use to execute code periodically.

See a demo and answer here for usage

Russ Cam
+1  A: 

You will want to have a look at setInterval() and setTimeout().

Here is a decent tutorial article.

Matthew Vines
+1  A: 

Since you want the function to be executed periodically, use setInterval

CMS
+15  A: 

You want setInterval():

var intervalID = setInterval(function(){alert("Interval reached");}, 5000);

The first parameter to setInterval() can also be a string of code to be evaluated.

You can clear a periodic function with:

clearInterval(intervalID);
zombat
I upvoted and then decided to rescind my upvote (but not downvote) because you are using a string argument to setInterval. Functions should almost always be used in favor of a string argument, for efficiency, security, and for their closure features.
Jason S
That's fine, I don't mind. `setInterval()` is the correct answer to the question, no matter the parameter. It is just an example, and both methods are by definition correct.
zombat
I agree with Jason S; it should really be a function. In your example, the only problem is a negligible performance loss, but it's a bad habit to get into.
Matthew Crumley
I agree with both of you, using a function is better. I never said it wasn't. It is correct either way, but for the sake of the comments, I will edit the answer to contain a function instead.
zombat
A: 
function test() {
 alert('called!');
}
var id = setInterval('test();', 10000); //call test every 10 seconds.
function stop() { // call this to stop your interval.
   clearInterval(id);
}
blesh
lol this one got answered quickly. heh. sorry for the double post.
blesh
+6  A: 

Everyone has a setTimeout/setInterval solution already. I think that it is important to note that you can pass functions to setInterval, not just strings. Its actually probably a little "safer" to pass real functions instead of strings that will be "evaled" to those functions.

// example 1
function test() {
  alert('called');
}
var interval = setInterval(test, 10000);

Or:

// example 2
var counter = 0;
var interval = setInterval(function() { alert("#"+counter++); }, 5000);
gnarf
+1 If you're a student of the [Crockford school of JavaScript](http://javascript.crockford.com/code.html), you avoid eval in all its evil forms.
Patrick McElhaney
@Patrick - I don't think the "Do not use $ (dollar sign) or \ (backslash) in names" suggestion will go down well with the jQuery lot :)
Russ Cam
A: 

The native way is indeed setInterval()/clearInterval(), but if you are already using the Prototype library you can take advantage of PeriodicalExecutor:

new PeriodicalUpdator(myEvent, seconds);

This prevents overlapping calls. From http://www.prototypejs.org/api/periodicalExecuter:

"it shields you against multiple parallel executions of the callback function, should it take longer than the given interval to execute (it maintains an internal “running” flag, which is shielded against exceptions in the callback function). This is especially useful if you use one to interact with the user at given intervals (e.g. use a prompt or confirm call): this will avoid multiple message boxes all waiting to be actioned."

Nelson