I need help getting my image slideshow to work properly. I set it up to loop over an array of images and display each one in 5 second intervals. What it actually does is loop over the entire array but only displays the last image. Code below:
const imgArray = ['http://media.tumblr.com/tumblr_lf88gg6J5d1qamm7n.jpg',
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR5SG1QIHcwLM9FNPFOO0IvFBNJ9CJCGZ-iGz7zfmfhTypEqqTU','https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQRZLiRBwwBBMAapL5IzxqoV-zskYuGD9lWvyirUryKjVaRqwlO'];
let index = 0;
function autoChange() {
let img = document.getElementById('img');
let arrayIndex;
for (arrayIndex = 0; arrayIndex < imgArray.length; arrayIndex++) {
img.src = imgArray[arrayIndex];
setTimeout(autoChange, 3000);
console.log(imgArray[arrayIndex]);
}
arrayIndex++;
if(arrayIndex > imgArray.length) {
arrayIndex = 0
};
}
autoChange();
<section>
<figure>
<img id='img' class='displayed-img' width="350" src="imgs/pic0.jpg">
</figure>
</section>
Thank you.