我的定时交通灯计划出了什么问题?

时间:2016-03-09 22:28:07

标签: javascript html traffic timed light

我不知道为什么因为它似乎都是正确的并且控制台中没有错误。它总是从green.png开始并留在那里?我正在尝试制作一个定时红绿灯序列,可以在没有按钮的情况下加载页面时立即启动。

<!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>

3 个答案:

答案 0 :(得分:1)

您的条件没有按照预期的方式执行,因为它们是使用赋值运算符“=”而非比较运算符“==”设置的。

答案 1 :(得分:1)

我已经清理了一下你的代码:

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. 如果您使用命名函数作为处理程序,则不要使用括号()

答案 2 :(得分:0)

这是工作和测试。包含以前的所有评论。

<!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);
                 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";
            }   
        }

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