Anonymous Anonymous - 1 month ago
201 0

Trying to create an automated traffic light sequence... I dont want to create it with CSS, I already have a working set of picture code - probably found on a post on here. Please help!

Javascript

Automated Traffic Light Sequence

<!DOCTYPE html>
<html>
<body>

<h1>HELP ME! :) </h1>

<p>This is my Traffic Light script</p>

<img id="light" src="./assets/red.jpg">

<button type="button" onclick="changeLights()">Change Lights</button>

<script>
var list = [
    "./assets/red.jpg",
    "./assets/green.jpg",
    "./assets/amber.jpg"
];

var index = 0;

function changeLights() {
    index = index + 1;

    if (index == list.length) index = 0;

    var image = document.getElementById('light');
    image.src=list[index];
}
</script>

</body>
</html>