帮助重置setTimeOut在javascript中制作交通灯
Help Reset setTimeOut for make a Traffic Light in javascript
我想让交通信号灯自动工作,就像我做的一样,但在19之后,它将重置并重新开始功能所以它会像交通灯一样永不停止,并且会重复他的自我
-
- 红灯10秒
-
- 2秒黄灯
-
- 6秒绿灯
-
- 1秒黄灯
-
- 和遣返回国 ........
function changecolor(){
var red = document.querySelector('#Red')
var yellow = document.querySelector('#Yellow')
var green = document.querySelector('#Green')
setTimeout(function(){ red.style.background= "red" },5); // on red
setTimeout(function(){ yellow.style.background= "yellow" //on yellow + off red
red.style.background= "#FF4A4D"
},10000);
setTimeout(function(){ yellow.style.background= "#F1FF73" //on green + off yellow
green.style.background= "green"
},12000);
setTimeout(function(){ yellow.style.background= "yellow" //on yellow + off green
green.style.background= "#43B560"
},18000);
setTimeout(function(){ red.style.background= "red" //off yellow + on red
yellow.style.background= "#F1FF73"
},19000);
setTimeout(changecolor(), 19005);
}
body{
width:100%;
height:100%;
margin: 0 auto;
}
h1{
margin-left:45%;
}
button{
margin-left:49%;
margin-top:2%;
color:white;
border: 1px solid blue;
background:black;
}
#Red{
display:block;
background:#FF4A4D;
width:15%;
height:20vh;
border-radius:50%;
margin-left:45%;
margin-top:1%;
}
#Yellow{
display:block;
background:#F1FF73;
width:15%;
height:20vh;
border-radius:50%;
margin-left:45%;
margin-top:1%;
}
#Green{
display:block;
background:#43B560;
width:15%;
height:20vh;
border-radius:50%;
margin-left:45%;
margin-top:1%;
}
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/script.js"></script>
<link rel="stylesheet" href="css/style.css" />
<title>Traffic Light</title>
</head>
<body>
<h1>Traffic Light</h1>
<div id="Red"></div>
<div id="Yellow"></div>
<div id="Green"></div>
<button onclick="changecolor()">Click To Start</button>
</body>
</html>
看一下这个例子。
function changecolor(){
var red = document.querySelector('#Red')
var yellow = document.querySelector('#Yellow')
var green = document.querySelector('#Green')
if(time == 20) {
red.style.background= "red";
yellow.style.background = "#F1FF73";
time = 1;
}
if(time % 10 == 0) {
yellow.style.background= "yellow"
red.style.background= "#FF4A4D"
}
if(time % 12 == 0) {
green.style.background= "green";
yellow.style.background = "#F1FF73";
}
if(time % 18 == 0) {
yellow.style.background= "yellow" //on yellow + off green
green.style.background= "#43B560"
}
if(time % 19 == 0) {
red.style.background= "red" //off yellow + on red
yellow.style.background= "#F1FF73"
}
time+=1;
}
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在JavaScript中将字符串转换为函数引用
- 模糊事件的Javascript测试
- Javascript更改图标
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何使用WCF服务和javascript表单post上传.doc文件
- javascript结合了数组和字典
- 这是什么 ==- javascript 运算符
- 从javascript创建一个列表
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在Javascript中将JSon对象转换为数组
- Javascript生成的表单未提交
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量