waxical
waxical

Reputation: 3896

Javascript: Force new loop iteration in setInterval

I have a setInterval loop. It's set to 3500 milliseconds, like so:-

var loop = setInterval(function() { /*stuff*/ }, 3500);

At one point in 'stuff' if a certain situation occurs, I want to force a new iteration of the loop and NOT WAIT for the 3500 milliseconds. How is that possible? Is it continue or do I just need to frame the process differently?

Upvotes: 1

Views: 1532

Answers (5)

Talha Akbar
Talha Akbar

Reputation: 10030

function looper(t) {
    var loop = setInterval(function() {
        document.write(s++);
        if (mycondition) { // here is your condition
            loopagain(200); // specify the time which new loop will do
            loop = window.clearInterval(loop); // clear the first interval
            return; // exit from this function!
        }
    }, t);
}

window.onload = looper(1000); // this will have default setInterval function time ans will start at window load!

function loopagain(t) {
    looper(t);
}​

http://jsfiddle.net/tFCZP/

Upvotes: 0

HMarioD
HMarioD

Reputation: 842

Use a named function and call it when you want.

var loop = setInterval(loopFunc, 3500);

function loopFunc(){
  //do something
}

function anticipate(){
  clearInterval(loop);  //Stop interval
  loopFunc();  //Call your function
  loop = setInterval(loopFunc, 3500);  //Reset the interval if you want
}

Upvotes: 1

Jared Farrish
Jared Farrish

Reputation: 49188

My contrived example:

var time = 3500,
    loops = 0,
    loop;

(function run(){
    var wait = time,
        dontwait = false;

    if (loops++ == 5) {
        loops = 0;
        dontwait = 1000;
    }

    console.log('Interval: ', dontwait || wait);

    return loop = setTimeout(run, dontwait || wait);
})();​

http://jsfiddle.net/NX43d/1/

Basically, a self-invoking function looping back on a self-calling function, with (!) shorthand variable switching. Nifty.

Upvotes: 0

ATOzTOA
ATOzTOA

Reputation: 35950

You can use something like this... using setTimeout instead of setInterval...

<script type="text/javascript">
    var show;
    var done = false;

    show = setTimeout(showHideForm, 3500);

    function showHideForm() {
        // Do something

        if(done) {
            clearTimeout(show);

            show = setTimeout(showHideForm, 2000);
        }
    }
</script>

clearTimeout takes as argument the handle which is returned by setTimeout.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

You could try writing an anonymous self-calling function using setTimeout instead of setInterval:

var i = 0;

(function() {
    // stuff
    i++;
    if (i % 2 == 0) {
        // If some condition occurs inside the function, then call itself once again
        // immediately
        arguments.callee();
    } else {
        // otherwise call itself in 3 and a half seconds
        window.setTimeout(arguments.callee, 3500);
    }
})();​ // <-- call-itself immediately to start the iteration

UPDATE:

Due to a disagreement expressed in the comments section against the usage of arguments.callee, here's how the same could be achieved using a named function:

var i = 0;
var doStuff = function() {
    // stuff
    i++;
    if (i % 2 == 0) {
        // If some condition occurs inside the function, then call itself once again
        // immediately
        doStuff();
    } else {
        // otherwise call itself in 3 and a half seconds
        window.setTimeout(doStuff, 3500);
    }
};
doStuff();

Upvotes: 2

Related Questions