Currently, I have to rebuild some pages to replace jQuery with vanilla JavaScript. In my case, I have a div wrapped around some images. The first image has a css-modifier (-active) and in JavaScript, I want a loop which deletes the modifier and gives it to the next image. In the end, the modifier should be placed on the first image again. This function should also run every second.
The html looks like this:
(function () {
var container = document.getElementsByClassName('m-weatherMap__image');
container = Array.from(container);
timedLoop();
function timedLoop () {
var i = 0;
setTimeout(function () {
i++;
timedLoop();
if (i < container.length) {
if (!container[i].classList.contains('-active')) {
setTimeout(fadeout, 300);
function fadeout () {
container[i - 1].classList.remove('-active');
container[i - 1].style.opacity = '0';
}
container[i].classList.add('-active');
}
}
}, 1000);
}
})();
<div class="m-weatherMap">
<img class="m-weatherMap__image -active" src="bla">
<img class="m-weatherMap__image " src="bla">
<img class="m-weatherMap__image " src="bla">
<img class="m-weatherMap__image " src="bla">
<img class="m-weatherMap__image " src="bla">
<img class="m-weatherMap__image " src="bla">
<img class="m-weatherMap__image " src="bla">
<img class="m-weatherMap__image " src="bla">
</div>
Now I want a loop which goes through the array and checks if the element has the modifier. If it does, it deletes it and gives it to the next element. At the moment, the class will be added to the second image but not further (yesterday it worked until the end of the array, not sure what I changed) but there it went to the end of the array and stopped there.
Does someone have an idea?
i = 0every timetimedLoop()is called. Instead of usingsetTimeout()why not usesetInterval()that way you don't have to keep callingtimedLoop()