0

I have following HTML code snippet:

<div>
    <div onload="startTimer()">
        <img id="img-showcase" src="/assets/img1.jpg">
    </div>
</div>

And the Javascript snippet:

<script type="text/javascript">

    function displayNextImage() {
        x = (x === images.length - 1) ? 0 : x + 1;
        document.getElementById("img-showcase").src = images[x];
    }

    function startTimer() {
        setInterval(displayNextImage, 3000);
    }

    var images = [], x = 0;
    images[0] = "/assets/img1.jpg";
    images[1] = "/assets/img2.jpg";
    images[2] = "/assets/img3.jpg";
</script>

I wish to make img change dinamically based on setInterval timer... What am i missing?

GreetingsFriend
  • 309
  • 1
  • 5
  • 17

1 Answers1

0

You can't add an "onload" event to a div. Try moving this to the body and it should work.

jas7457
  • 1,712
  • 13
  • 21