0

NB: I've had a look here for how to change the images on a timer, as oppose to using buttons, but it didn't cater to my code style.

As I'm new to HTML and JS I still don't understand all the aspects and got a little confused on which part they were referring to in translation to how I wrote my code.

CODE:

<!DOCTYPE html>
<html>
  <body>
    <img id="image" src="blank_light.jpg" style="width:100px">
    <p></p>
    <button class="change-image">Change Lights</button>
    <script>
      var imageSources = ["red_light.jpg", "red_and_yellow_light.jpg", "yellow_light.jpg", "green_light.jpg"]
      var buttons = document.getElementsByClassName("change-image")[0];
      var index = 0;

      buttons.addEventListener('click', function() {
        if (index === imageSources.length) {
          index = 0;
        }
        document.getElementById("image").src = imageSources[index];
        index++;
      });

    </script>
  </body>
</html>

I need to remove the buttons and have the images change on a timed basis instead. Ideally every 2 seconds.

Thanks in advance.

2
  • It's the same link he posted in the question, it's not the same problem Commented Feb 2, 2017 at 13:37
  • @ACOMIT001 Like Mattia kindly said, it's a different problem as the link I posted has a different original format as me. There's no point writing a bunch of code if I don't understand what it means. Commented Feb 2, 2017 at 13:40

2 Answers 2

8

You need to use setInterval function to change image after every 2 sec

<img id="image" src="blank_light.jpg" style="width:100px">
<p></p>

<script>

var imageSources = ["red_light.jpg", "red_and_yellow_light.jpg", "yellow_light.jpg", "green_light.jpg"]

var index = 0;
setInterval (function(){
  if (index === imageSources.length) {
    index = 0;
  }
  document.getElementById("image").src = imageSources[index];
  index++;
} , 2000);

</script>

CodePen : http://codepen.io/anon/pen/zNWMJK

Sign up to request clarification or add additional context in comments.

6 Comments

@asdf_enel_hak Thanks a lot both of you. Am I correct in saying I can remove the <(/)button>
The image gets stuck on the first one, without changing at all? Any ideas. Please feel free to proofread the code, bearing in mind I initially copied and pasted your work to Notepad to see if it worked.
@freddie.bumder : not getting you. Is it working or not?
No. The image is staying as the blank traffic light. Not changing to any of the others.
Where do I do that? In the original question as an update?
|
0
/CoffeeScript
$(document).ready ->
  imageSources = ["image1.jpg", "image2.png", "image3.png", "image4.png", "image5.png"]
  index = 0
  setInterval ( ->
    if index == imageSources.length
      index = 0
    $('#image-test').attr('src', imageSources[index]);
    index++
    return
  ), 2000

1 Comment

Please don't post code-only answers. Elaborate what your snippet does and why it solves the problem.

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.