0

Im tryin to create an image rotator where an image will change every 3 seconds.But the problem is, images aren't showing up. Here is my code:

    <html>


<script type='text/javascript'>

    var myScreen=document.getElementById("banner");
    var pictures=['stephIrv.jpg','pintball.jpg','badminton.jpg','running.jpg','boxing.jpg'];
    var ttlPics = pictures.length;
    var i=0;
    function slideShow()
    {
        if(i > (ttlPics - 1))
        {
            i=0;
        }
        myScreen.innerHTML = '<img src="'+pictures[i]+'">';
        i++;
        loopTimer = setTimeout(slideShow,3000);
    }
    slideShow();
</script>
<body>

<div id = 'banner'></div>


</body>

</html>
2
  • The first argument in setTimeout has to be a variable not a string. You don't have a loop function declared anywhere either. Commented Dec 7, 2015 at 19:08
  • Tried it but still not working :c Commented Dec 7, 2015 at 19:14

1 Answer 1

1

Your pictures need to be inside the same folder, as your script. But I guess you have that already.

First thing I would change, is to use slideShow in setTimeout instead of 'loop()'?

loopTimer = setTimeout(slideShow, 3000);

Hope that helps.

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

6 Comments

You are using 'slideShow()' in setTimeout as first parameter, try changing that to slideShow without the single-quotes and without the (). Just as I suggested in my answer. You need to use a callback here, not a string.
Can you share, what is happening, please? Are there any error-messages in your browser-console? What is missing?
There are no error message appearing in my browser. When I run my code, the browser is just a blank white space. No images are appearing.
UPDATE! I got it working by putting the entire <script> tag in the <body> after the <div>. Thanks for the help :)
Okay. When testing it, I actually did the same, but missed the point. Congrats!
|

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.