Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run setInterval several times, then pause for several seconds

I need my code to run x amount of times and then pause for 30 seconds or so before resuming. Any ideas?

myslidefunction();
var tid = setInterval(myslidefunction, 1000);

function myslidefunction() {
    setTimeout(function () {
        //do stuff  
    }, 400); 
};
like image 380
user1031699 Avatar asked Jun 04 '13 16:06

user1031699


2 Answers

You can keep a run-count, and use normal_duration + 30000 as the setTimeout delay for the X+ 1st time.

var runCount = 0, runsBeforeDelay = 20;
function myslidefunction(){

    // .. stuff

    runCount++;
    var delay = 0;
    if(runCount > runsBeforeDelay) {
        runCount = 0;
        delay = 30000;
    }
    setTimeout(myslidefunction, 400 + delay);
};

// start it off
setTimeout(myslidefunction, 1000);
like image 61
techfoobar Avatar answered Sep 24 '22 17:09

techfoobar


var counter = 0;

var mySlideFunction = function(){

    /* your "do stuff" code here */

    counter++;
    if(counter>=10){
        counter = 0;
        setTimeout(mySlideFunction, 30000);
    }else{
        setTimeout(mySlideFunction, 1000);
    }
}

mySlideFunction();
like image 33
CodeMoose Avatar answered Sep 22 '22 17:09

CodeMoose