以不同的时间间隔运行多个函数

Run multiple functions at different time intervals

本文关键字:运行 函数 时间      更新时间:2023-09-26

我有四个函数,

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

我要做的就是在不同的时间间隔内调用这些函数,也就是

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

我知道,setInterval(functionOne, time);可以在特定时间循环functionOne

如何使函数按照上面给出的持续时间顺序运行?谢谢。

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));
}

更新:

传递给函数的持续时间:

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));
}