user3348051
user3348051

Reputation:

Run multiple functions at different time intervals

I have four functions,

functionOne();
functionTwo();
functionThree();
functionFour();

All I want to do is to call these functions for different time durations, that is,

// run functionOne
functionOne();
// delay for a duration of 200ms

//run functionTwo
functionTwo();
// delay for a duration of 600ms

// run functionThree
functionThree();
// delay for a duration of 200ms

// run functionFour
functionFour();
// delay for a duration of 1600ms

// repeat from one again!

I know, setInterval(functionOne, time); can loop the functionOne at specific time.

How can I make the functions run in the order of duration given above? Thanks.

Upvotes: 0

Views: 1762

Answers (1)

Yangguang
Yangguang

Reputation: 1785

try:

var the_time = 1000;
var funArr = [funcitonOne,funcitonTwo,funcitonThree,funcitonFore];
for (var i=0; i<funArr.length;i++){
  setInterval(funArr[i], the_time*(i+1));
}

UPDATE:

duration be passed in to the function:

function funcitonOne(time) {
    console.log('funcitonOne', time);
}
function funcitonTwo(time) {
    console.log('funcitonTwo', time);
}
var funArr = [funcitonOne, funcitonTwo];
for (var i = 0; i < funArr.length; i++) {
    var interval = 500 * (i + 1);
    (function (i,interval) {
        setInterval(function(){
            funArr[i].call(this, interval);
        }, interval);
    }(i,interval));
}

Upvotes: 1

Related Questions