What's going wrong with my timed Traffic Lights program?

◇◆丶佛笑我妖孽 提交于 2019-12-13 08:48:10

问题


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>

回答1:


None of your conditionals are executing the way you intended since they are set up using the assignment operator "=" rather than comparative operator "==".




回答2:


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



回答3:


this is working and tested. incorporates all previous comments.

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


来源:https://stackoverflow.com/questions/35903955/whats-going-wrong-with-my-timed-traffic-lights-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!