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;
functions[functionIndex++]();
// what is happening in the line above? never seen square brackets follow with the > //parenthesis
if (timesRun === 2) {
clearInterval(myIntervals);
}
else if (functionIndex == functions.length)
functionIndex = 0;
// then what is happening in this other line above????
}, 185 * 1000);
}
starter();
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?