I'm currently working on a web widget that loads images from different directories based on the selection made in a drop-down list. If "Option 1" is chosen, it should fetch images from one directory at 7-second intervals. Choosing "Option 2" selects a different directory for image loading, also at 7-second intervals. I've implemented setInterval in JavaScript to achieve this functionality. While Option 1 works smoothly with the correct time interval, selecting Option 2 causes the interval to decrease from 7 seconds. I attempted to use clearInterval following various solutions, including guidance from this resource, but without success.
Here's a snippet of my code:
selectCity.addEventListener("change", () => {
celeb.src = `${directory}/image1.png`;
images = [];
images[0] = `/${directory}/image1.png`;
images[1] = `/${directory}/image2.png`;
images[2] = `/${directory}/image3.png`;
var setint = window.setInterval(changeImage, 7000);
var x = 1;
function changeImage() {
celeb.src = images[x];
console.log(images[x]);
x++;
if (images.length == x) {
x = 0;
window.clearInterval(setint);
setint = window.setInterval(changeImage, 7000);
}
}
}
I'm looking to utilize clearInterval each time "selectCity" changes. As a newcomer to HTML and JavaScript, I would appreciate any advice or suggestions on what might be wrong with my code.