javascript - 按下按钮时,如何使此交通灯序列重复多次>

标签 javascript html

document.getElementById('AllButton').onclick = switchAll;

function illuminateRed() {
  clearLights();
  document.getElementById('stopLight').style.backgroundColor = "red";
}

function illuminateOrange() {
  clearLights();
  document.getElementById('slowLight').style.backgroundColor = "orange";
}

function illuminateGreen() {
  clearLights();
  document.getElementById('goLight').style.backgroundColor = "green";
}

function illuminateRedOrange() {
  clearLights();
  document.getElementById('stopLight').style.backgroundColor = "red";
  document.getElementById('slowLight').style.backgroundColor = "orange";
}

function illuminateBlack() {
  clearLights();

}

function clearLights() {
  document.getElementById('stopLight').style.backgroundColor = "black";
  document.getElementById('slowLight').style.backgroundColor = "black";
  document.getElementById('goLight').style.backgroundColor = "black";
}

var clickTimes = 0;
var change = 1;

function switchAll() {
  clickTimes++;
  switch (clickTimes) {
    case 1:
      clearLights();
      document.getElementById('stopLight').style.backgroundColor = "red";
      break;
    case 2:
      clearLights();
      document.getElementById('stopLight').style.backgroundColor = "red";
      document.getElementById('slowLight').style.backgroundColor = "orange";
      break;
    case 3:
      clearLights();
      document.getElementById('goLight').style.backgroundColor = "green";
      break;
    case 4:
      clearLights();
      document.getElementById('slowLight').style.backgroundColor = "orange";
      break;
    case 5:
      clearLights();
      document.getElementById('stopLight').style.backgroundColor = "red";
      break;
    case 6:
      document.getElementById('stopLight').style.backgroundColor = "black";
      document.getElementById('slowLight').style.backgroundColor = "black";
      document.getElementById('goLight').style.backgroundColor = "black";
      break;

  }
}
body {
  font-family: sans-serif;
}
#controlPanel {
  float: left;
  padding-top: 30px;
}
.button {
  background-color: gray;
  color: white;
  border-radius: 10px;
  padding: 20px;
  text-align: center;
  margin: 90px 40px;
  cursor: pointer;
}
#traffic-light {
  height: 550px;
  width: 200px;
  float: left;
  background-color: #333;
  border-radius: 40px;
  margin: 30px 0;
  padding: 20px;
}
.bulb {
  height: 150px;
  width: 150px;
  background-color: #111;
  border-radius: 50%;
  margin: 25px auto;
  transition: background 500ms;
}
<div id="controlPanel">
  <h1 id="AllButton" class="button">Switch</h1>
</div>
<div id="traffic-light">
  <div id="stopLight" class="bulb"></div>
  <div id="slowLight" class="bulb"></div>
  <div id="goLight" class="bulb"></div>
</div>

这是我的 ICT 类(class)作业,我们必须构建一个交通信号灯系统,我的代码目前可以正常工作。但是,一旦我按下屏幕上的按钮并且每次点击都在可能的组合之间循环,交通灯就完成了它的序列,它不会再重复了。我想知道是否有人可以帮助我让它循环,我将不胜感激,谢谢,我是编码新手,如果它关闭了,我很抱歉。

最佳答案

只需在最后一个 break 之前写入 clickTimes = 0;

这是 fiddle :https://jsfiddle.net/7oa28cta/

关于javascript - 按下按钮时,如何使此交通灯序列重复多次>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37301948/

相关文章:

javascript - 根据下拉选项显示/隐藏

javascript - 相同编码的 div 不一样

javascript - 在两个单独的数组上嵌套 ng-repeat

javascript - 如何用html/css制作智能磁贴?

javascript - 如何在文本框中添加 "view:source"选项?

html - CSS - 如何使页面滚动到窗口边缘

javascript - 如何使用 jquery 或 javascript 将复选框选择限制为一个

html - 在 <INPUT> 字段中创建/放置 anchor <A HREF>

javascript - `if __name__ == ' __main__ '` 相当于 javascript es6 模块

php - 单独文件上的引导模式不会关闭