Can somebody tell me WHY this code works?

First, the code is working as intended, nothing wrong on performance at all… but there are a couple lines that I don’t know what is it doing.

Code below is to set a number of functions to run at a given interval of time using the

function text1() {
  console.log("here is my first text");

function text2() {
  console.log("here is my second text");

function starter() {
  var functions = [text1, text2];
  var timesRun = 0;
  var functionIndex = 0;
  var myIntervals = setInterval(function () {
    timesRun += 1;
    //  what is happening in the line above?  never seen square brackets follow with the >        //parenthesis
    if (timesRun === 2) {
    else if (functionIndex == functions.length)
      functionIndex = 0;
    //  then what is happening in this other line above????
  }, 185 * 1000);


I know it is doing some sort of loop, catching the needed functions in an array, breaking the loop after so many runs, and then what? it runs Ok but why?

functions [functionIndex++] ();

^^ that is a new one for me… calling the functions inside the array that way…

Also… interesting that they are actually working in the intervals given and not shooting them all at one time …

If I write the 2 separate functions inside the " setInterva()" method, it would just run the 2 functions immediately one after the other, and do the same in the next interval time…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.