Call js-function using JQuery timer

Viewed 196640

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');
}, 10000);

but this only executes once and then never again.

8 Answers

You can use this:

window.setInterval(yourfunction, 10000);

function yourfunction() { alert('test'); }
window.setInterval(function() {
 alert('test');
}, 10000);

window.setInterval

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

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

window.setInterval(function() {
    alert('test');
}, 10000);
function run() {
    window.setTimeout(
         "run()",
         1000
    );
}
Related