74
rated 0 times
[
79]
[
5]
/ answers: 1 / hits: 24971
/ 8 Years ago, thu, april 28, 2016, 12:00:00
Here is my code for a traffic light's sequences. I was wondering how I could add a timer in to change the traffic light colour every 3 seconds, for example, when the button is clicked. Thanks!
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Task 3</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/redamber.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>
More From » html