Skip to content Skip to sidebar Skip to footer

Execute Function Every Nth Second

I've made this snippet that clicks a link after 10th second: function timeout() { window.setTimeout(function() { $('img.left').click(); }, 1000); setTimeout('timeo

Solution 1:

Use setInterval instead of setTimeout

http://javascript.about.com/library/blstvsi.htm


Solution 2:

setInterval(yourFunction, 1000 * 10);

(time in miliseconds: 1000 is 1 second, 1000 * 10 is 10 seconds)

Which works better than "setTimeout"; more readable, etc.


Solution 3:

I use this approach for polling, recurring updates, and animations:

var timerId = setInterval(function() {

    if (timeToStopConditionMet) {
        clearInterval(timerId);
        return;
    }

    // your recurring code

}, 10000); // 10000 makes this code execute every 10 seconds

Solution 4:

Yes, setInterval(timeout, 1000) does nearly the same thing. It's somewhat different in that the the next interval starts counting immediately after 1000ms, not after the script that runs has completed (or even started). I advocate against it for precisely this reason, for most purposes. Your implementation is better, IMO.

Also, you don't need to pass the timeout function in a string, you can just pass the reference directly, i.e. setTimeout(timeout, 1000) instead of setTimeout("timeout()", 1000).


Solution 5:

window.onload=function(){
    GetCount(dateFuture1, 'countbox1');
}

Post a Comment for "Execute Function Every Nth Second"