0

I have tried many methods in order to get this to work however I just cant seem to get the image to change automatically in a set time, the code is:

<!DOCTYPE html>
<html>
    <body>
        <h1>JavaScript Task 3</h1>
        <p>This is my Traffic Light script</p>
        <img id="light" src="E:\A452\RED traffic light.jpg">
        <script>
        var list = [
            "RED AMBER traffic light.jpg",
            "GREEN traffic light.jpg",
            "AMBER traffic light.jpg",
            "RED traffic light.jpg"
        ];
        var index = 0;
        function changeLights() {
            index ++;
            if (index == list.length);
            index = 0;
            document.getElementById(light).src = list[index];
         }
         var timer = setInterval(changeLights,3000);
        </script>
    </body>
</html>
1
  • those file names are containing a whitespace. You should never use whitespaces in file names and you missed ' ' it should be getElementById('light') Commented Mar 3, 2017 at 12:55

2 Answers 2

2

There is an error here:

document.getElementById('light').src = list[index]; // Needs ''

Also note when your image changes you will loose the path in the image src property as they are not included in the array of lights.

Also

if (index == list.length); 
    index = 0;

change to

if (index == list.length) { 
    index = 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

To be honest, i wonder if the result is supposed to be just visual, or it has to be also "functional"... You could solve this visually simply by using GIF (yea, not the greatest), or via CSS - not an answer to your question per se. But it might be useful to know your options.

@keyframes red {
  0% {background: red;}
  33% {background: red;}
  34% {background: black;}
  100% {background: black;}
}

@keyframes yellow {
  0% {background: black;}
  33% {background: black;}
  34% {background: yellow;}
  66% {background: yellow;}
  67% {background: black;}
  100% {background: black;}
}

@keyframes green {
  0% {background: black;}
  66% {background: black;}
  67% {background: green;}
  100% {background: green;}
}

.trafficLight {
  height: 120px;
  width: 40px;
  padding:3px;
  background:#CCC;
}

.trafficLight div {
  width: 90%;
  height: 30%;
  margin:7% auto;
  border-radius: 50%;
  animation-duration: 5s;
  animation-iteration-count: infinite;
}

.red {animation-name: red;}
.yellow {animation-name: yellow;}
.green {animation-name: green;}
.trafficLight, .trafficLight div{
  box-shadow:inset 5px 5px 5px rgba(255,255,255,.5),
             inset -5px -5px 5px rgba(0,0,0,.5);
}
<div class="trafficLight">
  <div class="red"></div>
  <div class="yellow"></div>
  <div class="green"></div>
</div>

2 Comments

Fun fact: This whole animation is approx 0.9KB large. Scalable without any loss of detail.
it has to be with pictures and not a gif, I know there are easier ways to achieve the same thing but we are not allowed to use a gif or CSS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.