javascript - 在 Javascript 中设置交通灯的计时器
问题描述
我想为我的交通灯设置一个带有两个按钮的计时器。一键启动。另一个停止。
我希望当用户点击开始按钮时,交通灯开始工作并改变他的颜色,当用户点击停止时,交通灯也停止工作。我有一个代码,它只是自动工作而不是按钮。(即使用户没有点击任何按钮,圆圈也会自动改变颜色)
我如何设置该职责的按钮?
<button id="btn1">start</button>
<button id="btn2">stop</button>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<script>
const btn1=document.getElementById("btn1");
const btn2=document.getElementById("btn2");
const div1=document.getElementById("div1");
const div2=document.getElementById("div2");
const div3=document.getElementById("div3");
btn1.addEventListener("click",timerSet)
let timer= setInterval(timerSet,1500);
function timerSet(){
if(div1.style.backgroundColor=="red"){
div2.style.backgroundColor="yellow";
div1.style.backgroundColor="black";
div3.style.backgroundColor="black";
}
else if(div2.style.backgroundColor=="yellow"){
div3.style.backgroundColor="green";
div2.style.backgroundColor="black";
div1.style.backgroundColor="black";
}
else if (div3.style.backgroundColor=="green"){
div1.style.backgroundColor="red";
div3.style.backgroundColor="black";
}
}
Kreis(div1,20,"red");
Kreis(div2,20,"black");
Kreis(div3,20,"black");
function Kreis(element,radius,farbe){
element.style.backgroundColor=farbe;
element.style.width=2*radius+"px";
element.style.height=2*radius+"px";
element.style.borderRadius=2*radius+"px";
}
</script>
解决方案
这是我的解决方案,希望您满意。此致;)
const btn1 = document.getElementById("btn1");
const btn2 = document.getElementById("btn2");
const div1 = document.getElementById("div1");
const div2 = document.getElementById("div2");
const div3 = document.getElementById("div3");
let timer = null;
btn1.addEventListener("click", startTimer);
btn2.addEventListener("click", stopTimer);
function timerSet() {
if (div1.style.backgroundColor == "red") {
div2.style.backgroundColor = "yellow";
div1.style.backgroundColor = "black";
div3.style.backgroundColor = "black";
} else if (div2.style.backgroundColor == "yellow") {
div3.style.backgroundColor = "green";
div2.style.backgroundColor = "black";
div1.style.backgroundColor = "black";
} else if (div3.style.backgroundColor == "green") {
div1.style.backgroundColor = "red";
div3.style.backgroundColor = "black";
}
}
function Kreis(element, radius, farbe) {
element.style.backgroundColor = farbe;
element.style.width = 2 * radius + "px";
element.style.height = 2 * radius + "px";
element.style.borderRadius = 2 * radius + "px";
}
function startTimer() {
timer = setInterval(timerSet, 1500);
Kreis(div1, 20, "red"), Kreis(div2, 20, "black"), Kreis(div3, 20, "black");
}
function stopTimer() {
clearInterval(timer);
}
<button id="btn1">start</button>
<button id="btn2">stop</button>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
推荐阅读
- java - 在我的活动中执行定期工作请求时,工作管理器向 Worker 类发送错误的输入数据。如何解决?
- python - 多文件错误处理的最佳实践是什么?
- javascript - 我正在尝试获取嵌入的内容
- reactjs - 反应功能组件调用子组件方法
- swiftui - 如何在 SwiftUI 中创建一个获取 View 并返回自定义结果的函数?
- .htaccess - 当 public_html 上存在文件时,apache $1 失败
- ios - 使用代码交换检索令牌时,iOS 上的 Flutter Auth 挂起
- html - 为什么 devtool 中的搜索结果数量急剧下降?
- keyboard - 什么是'¬'字符,它是做什么用的
- html - 使用带有 flex-wrap: wrap 的 Flexbox 时如何使文本换行?