1

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?

2
  • 7
    Use a self-calling setTimeout instead. Much easier, cleaner, I prefer it anyhow. Commented Dec 28, 2012 at 13:08
  • The cleanest way would be implementing a method which calls itself under "certain circumstances". Commented Dec 28, 2012 at 13:09

5 Answers 5

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

I disagree with the use of arguments.callee. You should use named functions. I think the strict mode doesn't even allow the use of this property.
Alright, I've updated my post with an example of using a named function.
Oh, nice. My attempt is more or less the same (although I'm realizing I'm not saving any of the references to the setTimeout after the first... hmm). And what's with the goofy window.setTimeout?
2

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.

4 Comments

You can (function showHideForm(){})() so you don't have duplicated code snippets.
You have show = setTimeout(showHideForm, #000); twice. DRY, right?
@JaredFarrish How else do I do this?
Put ()() around showHideForm(){} (the first parenthesis). Some people call it an immediately invoked function or a self-invoking function. It's not a big deal, but when I see that micro repitition in my own code, it always annoys me the way small things do, you know? Anyways, you can see them in action in my answer and Darin's.
1

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
}

Comments

0

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.

Comments

0
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.