我必须创建一个自动灯光序列,我有一个会在点击一个按钮时改变,但我似乎无法让它自动工作。代码是:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Task 3</h1>
<p>This is my Traffic Light script</p>
<img id="light" src="E:\A452\RED traffic light.jpg">
<button type="button" onclick="changeLights()">Change Lights</button>
<script>
var list = [
"RED AMBER traffic light.jpg",
"GREEN traffic light.jpg",
"AMBER traffic light.jpg",
"RED traffic light.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>