如何在我的红绿灯脚本中添加计时器?

时间:2017-02-17 09:51:22

标签: javascript

我尝试了很多不同的方法但是我没有成功



var list = [
  "https://assets.publishing.service.gov.uk/media/559fbe1940f0b6156700004d/traffic-light-red.jpg",
  "https://assets.publishing.service.gov.uk/media/559fbe48ed915d1592000048/traffic-light-amber.jpg",
  "https://assets.publishing.service.gov.uk/media/559fbe3e40f0b6156700004f/traffic-light-green.jpg",
  "https://assets.publishing.service.gov.uk/media/559fbe48ed915d1592000048/traffic-light-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];
}

<h1>WORK</h1>

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

<img id="light" src="https://assets.publishing.service.gov.uk/media/559fbe1940f0b6156700004d/traffic-light-red.jpg">

<button type="button" onclick="changeLights()">Change Lights</button>
&#13;
&#13;
&#13;

0 个答案:

没有答案