views:

15359

answers:

7

Is there anyway to implement a timer for JQuery, eg. every 10 seconds it needs to call a js function.

I tried the following

window.setTimeout(function() {
 alert('test');
}, 1000);

but this only executes ones and then never again.

+7  A: 

setInterval is the function you want. That repeats every x miliseconds.

window.setInterval(function() {
    alert('test');
}, 1000);
Ikke
+14  A: 
window.setInterval(function() {
 alert('test');
}, 10000);

window.setInterval

Calls a function repeatedly, with a fixed time delay between each call to that function.

rahul
Could you please edit to 10000? That is 1 second and not 10.
Artur Carvalho
+19  A: 

You can use this:

window.setInterval(yourfunction, 10000);

function yourfunction() { alert('test'); }
Kristof Claes
better is `window.setInterval(yourFunction, 1000);` the name will call it and in quotes it goes through an eval and slows down.
Rixius
Should be 10000 and not 1000 for 10s.
Paulo Manuel Santos
You're right, Paulo. I've edited my answer. The 1.000 was there because I just copied it from Roland's question.
Kristof Claes
A: 
function run() {
    window.setTimeout(
         "run()",
         1000
    );
}
harpax
+1  A: 

try jQueryTimers, they have great functionality for polling

http://plugins.jquery.com/project/timers

Eggie
+3  A: 

jQuery 1.4 also includes a .delay( duration, [ queueName ] ) method if you only need it to trigger once and have already started using that version.

$('#foo').slideUp(300).delay(800).fadeIn(400);

http://api.jquery.com/delay/

Ooops....my mistake you were looking for an event to continue triggering. I'll leave this here, someone may find it helpful.

Cedar Teeth
+1  A: 

I was just experimenting with jQuery and wrote a simple timer / stop watch ... http://bornagainprogrammer.net/2010/08/19/a-simple-stop-watch-timer-using-jquery/

kiran