使用javascript的交通灯

编程入门 行业动态 更新时间:2024-10-26 12:35:31
本文介绍了使用javascript的交通灯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想创建交通灯控制器。

<!DOCTYPE html> < html lang =en> < head> < meta charset =UTF-8> < title> isiqfor< / title> < link rel =stylesheethref =style.css> < / head> < body onload =timer;> < div id =isiqfor> < div class =green>< / div> < div class =yellow>< / div> < div class =red>< / div> < / div> < script src =script.js>< / script> < / body> < / html>

CSS code:

#isiqfor { border:10px纯黑色; padding:10px 3px; width:50px; } #isiqfor> div { width:50px; height:50px; border-radius:50%; 不透明度:.3; } .green { background-color:green; } 。黄色{ background-color:yellow; } .red { background-color:red; }

和JS文件:

function myFun(){ // body ... var green = document.getElementsByClassName(green)[0]; var red = document.getElementsByClassName(red)[0]; var yellow = document.getElementsByClassName(yellow)[0]; green.style.opacity = 1; setTimeout(function(){ / * body ... * / green.style.opacity = .3; red.style.opacity = .3; yellow.style.opacity = 1; },5000); $ b $ setTimeout(function(){ / * body ... * / green.style.opacity = .3; red.style.opacity = 1 ; yellow.style.opacity = .3; },7000); $ b setTimeout(function(){ / * body ... * / green.style.opacity = 1; red.style.opacity = .3 ; yellow.style.opacity = .3; },12000); $ b var timer = setInterval(function(){ / * body ... * / myFun()},13000);

但问题是当页面加载时,它必须等待13秒开始交通灯。如何解决这个问题?我希望当页面加载绿灯已经切换。

你有没有试过调用 myFun 在你的计时器被设置后马上消失?查看对 myFun 添加到以下代码底部的调用:

var timer = setInterval(function(){ / * body ... * / myFun()},13000); myFun(); //立即呼叫'myFun'...

I want create traffic light controller.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>isiqfor</title> <link rel="stylesheet" href="style.css"> </head> <body onload="timer;"> <div id="isiqfor"> <div class="green"></div> <div class="yellow"></div> <div class="red"></div> </div> <script src="script.js"></script> </body> </html>

CSS code:

#isiqfor{ border: 10px solid black; padding: 10px 3px; width: 50px; } #isiqfor>div{ width:50px; height: 50px; border-radius: 50%; opacity: .3; } .green{ background-color: green; } .yellow{ background-color: yellow; } .red{ background-color: red; }

And JS file:

function myFun () { // body... var green=document.getElementsByClassName("green")[0]; var red=document.getElementsByClassName("red")[0]; var yellow=document.getElementsByClassName("yellow")[0]; green.style.opacity=1; setTimeout(function () { /* body... */ green.style.opacity=.3; red.style.opacity=.3; yellow.style.opacity=1; },5000); setTimeout(function () { /* body... */ green.style.opacity=.3; red.style.opacity=1; yellow.style.opacity=.3; },7000); setTimeout(function () { /* body... */ green.style.opacity=1; red.style.opacity=.3; yellow.style.opacity=.3; },12000); } var timer = setInterval(function () { /* body... */ myFun() },13000);

But problem is when page loads it must wait 13 second for beginning traffic light.How can solve this problem? I want when page loads green light has switched.

解决方案

Have you tried calling myFun straight away after your timer is set? See the call to myFun added to the bottom of the following code:

var timer = setInterval(function () { /* body... */ myFun() },13000); myFun();//Call 'myFun' straight away...

更多推荐

使用javascript的交通灯

本文发布于:2023-10-30 11:32:10,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:交通灯   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!