Right then, I'm having a little bit of a problem with some animation using JavaScript and the DOM. I first created a little page and script that mimics a set of traffic lights (UK traffic lights at least - Only one light is allowed on at any one time, so if amber is lit up then red and green must be out and so on.) that the user could control by clicking the appropriate link. See my Traffic Lights Controller page. Right click, view the source, and click on the external JS file to see the script.It looks messy now but I'm going to post a separate JS Code Review for that script later, for now, I want to concentrate on the next stage...
What I've been trying to do (and failing miserably at) is to remove the controls and have the traffic lights automated so when you load up the page it goes through each of them being on whilst the others are off. I'm pretty sure that the mistake I've made with this new script is minimal or I've only missed something small out (or otherwise I was totally off the ball!) but if somebody could help me to get this work then I'd be very grateful of the help.
Automatic Traffic Lights [HTML]
Code HTML4Strict:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Automatic Traffic Lights</title> <link rel="stylesheet" type="text/css" media="screen" href="stylesheet.css"> <script type="text/javascript" src="automatic-traffic-lights.js"></script> </head> <body> <div id="wrapper"> <h1>Automatic Traffic Lights</h1> <div id="red-light"></div> <div id="amber-light"></div> <div id="green-light"></div> </div> </body> </html>
Automatic Traffic Lights [JS]
Code JavaScript:function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } } function trafficLightsControllerAutomated() { if (!document.getElementById) return false; var greenLight = document.getElementById("green-light"); var amberLight = document.getElementById("amber-light"); var redLight = document.getElementById("red-light"); redLight.style.height = "20px"; redLight.style.width = "20px"; redLight.style.margin = "1px 0"; redLight.style.backgroundColor = "black"; amberLight.style.height = "20px"; amberLight.style.width = "20px"; amberLight.style.margin = "1px 0"; amberLight.style.backgroundColor = "black"; greenLight.style.height = "20px"; greenLight.style.width = "20px"; greenLight.style.margin = "1px 0"; greenLight.style.backgroundColor = "black"; function changeLightsToRed() { greenLight.style.backgroundColor = "black"; amberLight.style.backgroundColor = "black"; redLight.style.backgroundColor = "red"; setTimeout ("changeLightsToAmber()", 2000); } function changeLightsToAmber() { greenLight.style.backgroundColor = "black"; amberLight.style.backgroundColor = "orange"; redLight.style.backgroundColor = "black"; setTimeout ("changeLightsToGreen()", 2000); } function changeLightsToGreen() { greenLight.style.backgroundColor = "green"; amberLight.style.backgroundColor = "black"; redLight.style.backgroundColor = "black"; setTimeout ("changeLightsToAmberTwo()", 2000); } function changeLightsToAmberTwo() { greenLight.style.backgroundColor = "black"; amberLight.style.backgroundColor = "orange"; redLight.style.backgroundColor = "black"; setTimeout ("changeLightsToRed()", 2000); } changeLightsToRed(); changeLightsToAmber(); changeLightsToGreen(); changeLightsToAmberTwo(); } addLoadEvent(trafficLightsControllerAutomated);
Cheers,
Andrew Cooper



It looks messy now but I'm going to post a separate JS Code Review for that script later, for now, I want to concentrate on the next stage...
Reply With Quote



Bookmarks