I a have a list of colors in an array and I would like to make a function that goes through the array and takes the color that is associated with the index of the array to change the background of the div every second. The color of the div should depend on where the index of the array is.
$(document).ready(function(){
var array = ["red", "blue", "yellow"];
var counter = 0;
var nextColor;
function bgchange() {
$(".box").css("backgroundColor", "");
counter = (counter + 1) % array.length;
nextColor = array[counter];
$(".box").css("backgroundColor","'" + nextColor +"'");
}
setInterval(bgchange, 1000)
});
I want to accomplish this task by using the code that is similar to the one above but that actually works. Thank you for your help in advance.