title: I have been makign a code with javascript that run o. while I have made it run n
body: <p>I have been writing Javascript code to show a traffic light with a changing image. It uses a set array but I am new to coding and have not been told how to do this. I have written the code below but it is still not working. I can get it to run by clicking it but I need it to run without having to press the button lots of times.</p>
  
  <p>It changes between images that are in the same folder as the code. I want it to work by a person only pressing the button once.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;img id="light" src="traffitlr.jpeg"&gt;
  
  &lt;button type="button" onclick="changeLights()"&gt;changelights(and Start Lights)&lt;/button&gt;
  &lt;button type="button" onclick=setinterval(change,1000)&gt;start auto cycle&lt;/button&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>var list = [
      "traffitlg.jpg",
      "traffitla.jpg",
      "traffitlr.jpg",
      "traffitly.jpg"
  ];
  
  
  var index = 0;
  
  function changeLights() {
      index = index + 1;
  
      if (index == list.length) index = 0;
  
      var image = document.getElementById('light');
      image.src=list[index];
  }
  </code></pre>