I’m trying to make interval function, which will change selected div every 10seconds. When i’m on first page, i want it to show items from first array, when im on second i want array 2, etc. this is what i got so far:
var timedFunc = '';
var index= new Array(20)
index[0]="index1";
index[1]="index2.";
..
var indextwo= new Array(20)
indextwo[0]="index1";
indextwo[1]="index2";
var tmp = 0;
function display_index(nameofarray) {
if (tmp < 0) { tmp = nameofarray.length-1; }
if (tmp > nameofarray.length-1) { return false; }
document.getElementById('robot').innerHTML = nameofarray[tmp];
tmp = tmp + 1;
}
function indexInterval(m) {
switch(m) {
case 1: timeFunc = setInterval("display_index(index)",1000);
case 2: timeFunc = setInterval("display_index(indextwo)",1000);
case 3: timeFunc = setInterval("display_index(indexthree)",1000);
case 4: timeFunc = setInterval("display_index(indexfour)",1000);
}
}
To cycle through an array on an interval timer, you can pass the desired array to a function and then use that function to cycle through the array on an interval timer like this:
This type of implementation avoids the
switchstatement and avoids passing text tosetInterval(). It also uses a function closure to keep track of the state that thesetInterval()callback needs.