Problems with the time interval on a carousel

Asked

Viewed 316 times

0

Well, I’m having some problems with a self-made carousel, more specifically in the transition order of the items.

var carouselBannerHolder = document.getElementById("banners-slider");
var itemsCarousel = document.querySelectorAll(".banners-slider figure");
var count = 0;    
carouselBannerHolder.style.width = (parseInt(itemsCarousel.length)) + "00%";

function ordemComumSlide(){
    setInterval(function(){
        if(count < itemsCarousel.length){
            carouselBannerHolder.style.left = ("-" + count + "00%");
            count++;
        }else{
            return count = 0;
        }
    },2500);
};
ordemComumSlide();

On the first slide, it takes twice as long when the page starts, then works normally, how can I fix ? there’s something in the code too, in which I can improve ?

  • but the problem is not in html, it’s just the same js, because, I wanted to know why it takes twice as long to load the first slide backwards

  • I don’t think HTML would help much, because it’s a div with figures inside

  • I’ll edit with the functions add-ons, I forgot that

1 answer

2


Deep down, what’s going on with the first lap is that she’s going to do the following:

carouselBannerHolder.style.left = ("-000%");

because Count is still 0, that is this return apparently takes twice as long as the others, starts and resets Count to 1:

count = 1;
...
else {
     carouselBannerHolder.style.left = ("0");
     count = 1;
}
...

And I think that’s settled, ex:

var len = 3;
var count = 1;
function ordemComumSlide(){
    setInterval(function(){
        if(count < len){
           console.log("-" + count + "00%");
           count++;
        }else{
           console.log("0");
           count = 1;
        }
    },1500);
};
ordemComumSlide();

You can also choose another approach, I would do so:

var len = 3;
var count = 0;
function ordemComumSlide(){
    setInterval(function(){
        count++;
        if(count >= len){
           count = 0;
        }
        //carouselBannerHolder.style.left = ("-" + count + "00%");
        console.log("-" + count + "00%");
    },1500);
};
ordemComumSlide();

  • solved in the first banner, but in return it doubles the time now :/

  • 1

    I’ll see what might be happening @Murilogambôa

  • here, not knowing why I had a small bug, still in the last banner timer, so instead of putting the full length, I subtracted 1, but did not read the last banner, so I subtracted only . 5, and is working normal now

  • 1

    How, @Murilogambôa? Some adjustment needs to be made to the answer?

  • no, the right answer :D was more even a problem in my agr elements ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.