我的定时交通灯程序出了什么问题? [英] What's going wrong with my timed Traffic Lights program?

查看:157
本文介绍了我的定时交通灯程序出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么因为它似乎都是正确的并且控制台中没有错误。它总是从green.png开始并停留在那里?我试图制作一个定时的交通信号灯序列,只要页面没有按钮就可以启动。

I don't know why as it seems to be all correct and there are no errors in console. It always starts on green.png and stays there? I'm trying to make a timed traffic lights sequence that can start as soon as the page is loaded without a button.

<!DOCTYPE html>
<html>
<head>
</head>
    <body>
        <h2>Traffic Lights Program</h2>
        <div class="light"><img src="Blank.png" style="width:100px;height:228px;"/></div>

        <script>
        trafficLight = "green";
        var trafficLights = ["Red.png","RedYellow.png","Yellow.png","Green.png"]
        function green() {
            document.images[0].src = trafficLights[3];
        }

        function yellow() {
            document.images[0].src = trafficLights[3];
        }

        function redYellow() {
            document.images[0].src = trafficLights[1];
        }

        function red() {
            document.images[0].src = trafficLights[0];
        }

        function yellow2() {
            document.images[0].src = trafficLights[2];
        }

        function automatic() {
            if (trafficLight = "green") {
                setTimeout(green(),500)
                var trafficLight = "yellow";
            } else if (trafficLight = "yellow") {
                setTimeout(yellow(),500)
                var trafficLight = "redYellow";
            } else if (trafficLight = "redYellow") {
                setTimeout(redYellow(),500)
                var trafficLight = "red";
            } else if (trafficLight = "red") {
                setTimeout(red(),500)
                var trafficLight = "yellow2";       
            } else {
                setTimeout(yellow2(),500)
                var trafficLight = "green";
            }   
        }

        setInterval(automatic(),1000)
    </script>
    </body>
</html>


推荐答案

/ b>

I've cleaned up your code a bit:

function automatic() {
    if (trafficLight == "green") {
        setTimeout(green,500);
        trafficLight = "yellow";
    } else if (trafficLight == "yellow") {
        setTimeout(yellow,500);
        trafficLight = "redYellow";
    } else if (trafficLight == "redYellow") {
        setTimeout(redYellow,500);
        trafficLight = "red";
    } else if (trafficLight == "red") {
        setTimeout(red,500);
        trafficLight = "yellow2";       
    } else {
        setTimeout(yellow2,500);
        trafficLight = "green";
    }   
}

var interval = setInterval(automatic,1000);




  1. 不要用 var 多次。

  2. 对于语句使用 == 运算符。

  3. 如果使用命名函数作为处理程序,请不要使用括号()

  1. Don't redeclare vars with var multiple times.
  2. Use the == operator for statements.
  3. Don't use brackets () if you using a named function as handler.

这篇关于我的定时交通灯程序出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆