1

I'm trying to rotate a set of 6 images in javascript, not jquery. The first image shows up, but the rest of them don't rotate. Here's my code:

<html>
<head>
<script type="text/javascript">
function rotatePic() {

var qutAd = document.getElementById("yumOreos");
var imgs = ["images/img1.png", "images/img2.png", "images/img3.png", "images/img4.png", "images/img5.png", "images/img6.png"];
var ad = 0;

qutAd.src = imgs[ad];

setInterval(function () {ad++;if (ad == imgs.length) {ad = 0;}}, 4000);
}  

</script>
</head>
<body onload="rotatePic()">
     <img id="yumOreos">
</body>
</html>

What am I doing wrong? Thanks.

2 Answers 2

1

You have to set the src in the interval function as well.

setInterval(function () {
    ad++;
    if (ad == imgs.length) ad = 0;
    qutAd.src = imgs[ad];
}, 4000);
Sign up to request clarification or add additional context in comments.

Comments

0

You have forgotten to change the index of the array, to change the source. So you can do this

setInterval(function () {if (ad == imgs.length) {ad = 0;} qutAd.src =imgs[ad++]}, 4000);
} 

Comments

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.