0

I want to execute a series of functions in order and at 1-second interval, and I write 2 functions main1 and main2 to do this:

function main1()
{
    for (var i = 0; i < 3; i ++)
    {
        eval("myFunc_" + i + "()");
        myPause(1000);
    }
}

function myPause(x)
{
  var start = new Date().getTime();
  for (var j = start; j < start + x; j = new Date().getTime())
  {
    ;
  }
}

function main2()
{
    for (var i = 0; i < 3; i ++)
    {
        setTimeout("myFunc_" + i + "()", i * 1000);
    }
}

I write a pause function myPause for main1 and I think these 2 functions are doing the same thing, but the function main1 doesn't work well. What's the problem of the function main1?

NiaBie
  • 55
  • 1
  • 9

2 Answers2

0

I'm not sure why the main1 function does not work, but I see a couple of problems:

  • Implementing a pause like myPause blocks the execution and wastes cpu time. You should use something like setTimeout.

  • You should avoid the use of eval, see this question for more.

  • Why the outer for in myPause? is waiting twice?

I'm not sure what you want to accomplish, but if you want something like:

  • at second 1, call myFunc_1

  • at second 2, call myFunc_2

  • so on... (and go around in the list without stopping)

You can do something like this:

function myFunc1() { console.log('called myFunc1') }

function myFunc2() { console.log('called myFunc2') }

function myOtherFunc() { console.log('called myOtherFunc') }

const myFunctions = [
  myFunc1,
  myFunc2,
  myOtherFunc,
];

let counter = 0;
function runMyNextFunction() {
  let func = myFunctions[counter];

  func();

  counter += 1;
  if (counter >= myFunctions.length) {
    counter = 0;
  }
}

setInterval(runMyNextFunction, 1000);
pdpino
  • 444
  • 4
  • 13
  • The outer for loop is a mistake, I have deleted it. – NiaBie Apr 14 '19 at 07:30
  • I use the function `myPause` instead of `setTimeout` is because I want the function to stay suspend until `myPause` has finished – NiaBie Apr 14 '19 at 07:35
  • If you use something like `myPause` the function will stay suspended for that time, literally doing nothing (the processor will be occuppied doing nothing). It looks like you want something like a `sleep()` function from other languages, which makes the execution wait for an amount of time, but the processor can do other things in the meantime. If so, you can use JS promises to implement it like shown [here](https://stackoverflow.com/a/39914235/9951939). – pdpino Apr 15 '19 at 03:05
0

There is a lot of methods to do that. this is one of them:

function starter(){
    var functions = [main1, main2 /*,...*/];
    var functionIndex = 0;
    setInterval(function(){
        functions[functionIndex++]();
        if(functionIndex == functions.length)
            functionIndex = 0;
    },1000);
}

function main1(){
    console.log('main1 executed');
}

function main2(){
    console.log('main2 executed');
}

// start the program
starter();

important: you can use setTimeout instead of setInterval if you want to execute the queue only once.

Mohi
  • 1,776
  • 1
  • 26
  • 39