As you wanted to display 3
elements from the array each time and when the last element is displayed you want to repeat the process again you are suppose to use setInterval()
to call the function after a specified time continuously and reset the index to 0 immediately after the last element is displayed. Else increment the initial or counter value. Try this way,
var array = [1,2,3,4,5];
var initVal = 0;
setInterval(function(){
console.log(array[initVal] + "," + array[initVal+1] + "," + array[initVal+2]);
initVal = initVal == 2 ? 0 : initVal+1;
}, 500);
solved how to display all elements of an array in javascript [closed]