Question

I have this code:

var int1 = setInterval(function () {
    // do stuff

    if(//stuff done){
        clearInterval(int1); 
        setTimeout(
             function () {
                  setInterval(int1)
             }
        ,60000);
    }}
}, 1000)

and want the interval to be running again after 60 seconds but setInterval(int1) doesn't seem to trigger it again. What am I doing wrong?

EDIT: full code: http://pastie.org/8704786

Was it helpful?

Solution

That'd because int1 is not a function, but an interval id. Try this instead:

var int1;
var func = function () {
    // do stuff

    if(//stuff done){
        clearInterval(int1); 
        setTimeout(func, 60000);
    }
};
int1 = setInterval(func, 1000);

OTHER TIPS

You did 2 mistakes:

  1. setInterval whant a function, while int1 contains an interval handle
  2. You didn't pass amount of time in your setInterval call

What you want probably is:

var int1;

function scheduleStuff() {
    int1 = setInterval(doStuff, 1000);
}


function doStuff() {
    // do stuff

    if(/*stuff done*/){
        clearInterval(int1); 
        setTimeout(scheduleStuff,60000);
    }}
}

scheduleStuff();

set intervall expectes a function wich is called after waiting time...

this line is wrong:

setInterval(int1)

no function and no waiting time given...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top