My HTML has 5 images of lightOff.jpg bulbs + 1 button.
My mission is to press a button and then turn on the lights (change the images to lightOn.jpg) one by one every of 1 second. Meaning 1 bulb will on and after 1 second the next bulb will on and so on…. This is what I try to start….
function turnOnLights() {
var bulb = ["one", "two", "three", "four"];
for (var i = 0; i < bulb.length; i++) {
setTimeout(function() {
document.getElementById(bulb[i]).src = "https://maxcdn.icons8.com/app/uploads/2015/12/light_on-1.png"
}, 1000);
}
};
#containger {
text-align: center;
vertical-align: middle;
line-height: 30px;
margin-top: 20px;
}
<div id="containger">
<img src="https://maxcdn.icons8.com/app/uploads/2015/12/light_off-1.png" id="one">
<img src="https://maxcdn.icons8.com/app/uploads/2015/12/light_off-1.png" id="two">
<img src="https://maxcdn.icons8.com/app/uploads/2015/12/light_off-1.png" id="three">
<img src="https://maxcdn.icons8.com/app/uploads/2015/12/light_off-1.png" id="four">
<br>
<button type="button" onclick="turnOnLights();">Click to turn on lights</button>
</div>