首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何自动执行此红绿灯序列?

如何自动执行此红绿灯序列?
EN

Stack Overflow用户
提问于 2016-05-24 21:09:08
回答 1查看 152关注 0票数 2

代码语言:javascript
复制
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";
      clickTimes = 0
      break;
  }
}
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

你好,这是我的红绿灯序列,它工作得很好,但是我想知道如何将它自动化,一个按钮说停止,另一个按钮说Go,当go被按下时,它会一直循环?提前感谢您的阅读。

EN

回答 1

Stack Overflow用户

发布于 2016-05-24 22:57:56

正如评论中所说,我会使用setInterval。但是为了把这个放到代码中,我做了这个例子。此外,我将灯光放入对象中,并使其更紧凑。

代码语言:javascript
复制
/*
  neat light object
*/
function Light(color, element) {
  this.color = color;
  this.element = document.getElementById(element);
}
Light.prototype = {
  on: function() {
    this.element.style.backgroundColor = this.color;
  },
  off: function() {
    this.element.style.backgroundColor = "black";
  }
}

/*
init
*/
var lights = [new Light("green", "goLight"),
  new Light("orange", "slowLight"),
  new Light("red", "stopLight")
];

var sequence = [["green"],["green", "orange"],["red"]],
    position = 0,
    timer = null;

var go = document.getElementById("go"),
  stop = document.getElementById("stop");

go.onclick = function() {
  clearTimeout(timer);
  timer = setInterval(function() {

    lights.forEach(function(light) {
      if (sequence[position].indexOf(light.color) > -1) light.on();
      else light.off();
    });
    
     if (position++ >= sequence.length-1) position = 0;
   

  }, 2000);
}
stop.onclick = function() {
  clearTimeout(timer);
}
代码语言:javascript
复制
 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;
} 
代码语言:javascript
复制
    <div id="controlPanel">
        <h1 id="go" class="button">Go</h1>
        <h1 id="stop" class="button">Stop</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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37414613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档