Call js-function using JQuery timer

JavascriptJquery

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

You can use this:

window.setInterval(yourfunction, 10000);

function yourfunction() { alert('test'); }

Solution 2 - Javascript

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

window.setInterval

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

Solution 3 - Javascript

Might want to check out jQuery Timer to manage one or multiple timers.

http://code.google.com/p/jquery-timer/

var timer = $.timer(yourfunction, 10000);

function yourfunction() { alert('test'); }

Then you can control it with:

timer.play();
timer.pause();
timer.toggle();
timer.once();
etc...

Solution 4 - Javascript

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

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

try jQueryTimers, they have great functionality for polling

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

Solution 7 - Javascript

You can use setInterval() method also you can call your setTimeout() from your custom function for example

function everyTenSec(){
  console.log("done");
  setTimeout(everyTenSec,10000);
}
everyTenSec();

Solution 8 - Javascript

function run() {
    window.setTimeout(
         "run()",
         1000
    );
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElitmiarView Question on Stackoverflow
Solution 1 - JavascriptKristof ClaesView Answer on Stackoverflow
Solution 2 - JavascriptrahulView Answer on Stackoverflow
Solution 3 - JavascriptjchavannesView Answer on Stackoverflow
Solution 4 - JavascriptIkkeView Answer on Stackoverflow
Solution 5 - JavascriptCraigView Answer on Stackoverflow
Solution 6 - JavascriptEggieView Answer on Stackoverflow
Solution 7 - JavascriptAren HovsepyanView Answer on Stackoverflow
Solution 8 - JavascriptharpaxView Answer on Stackoverflow