I need to make sure when the button is clicked the images change but when I test the code the images change but they stop after one cycle. Please give some possible solutions on how to add a loop or make the code repeat ,thanks.
var red = "data:image/gif;base64,R0lGODlhAQABAIABAP8AAAAAACwAAAAAAQABAAACAkQBADs=",
redorange = "data:image/gif;base64,R0lGODlhAQABAIABAP9aAAAAACwAAAAAAQABAAACAkQBADs=",
green = "data:image/gif;base64,R0lGODlhAQABAIABAAD/AAAAACwAAAAAAQABAAACAkQBADs=",
orange = "data:image/gif;base64,R0lGODlhAQABAIABAP+qAAAAACwAAAAAAQABAAACAkQBADs=";
var lights = [red, redorange, green, orange];
var lightCounter = 0;
function pictureChange(){
lightCounter += 1;
document.getElementById('traffic').src = lights[lightCounter];
}
h1 {
text-align: center;
}
img {
display: block;
margin: 0 auto;
}
<h1><b> Traffic Lights </b></h1>
<center>
<button onclick="pictureChange()">Change Lights</button>
</center>
<br><br>
<img width="230" height="400" id="traffic" src="data:image/gif;base64,R0lGODlhAQABAIABAP8AAAAAACwAAAAAAQABAAACAkQBADs=">