Carousel cancel interval when I click on a btn

I don’t know how to program this code so every time I click on BTNRIGHT or BTNLEFT the 3000ms interval restarts.
Because the problem is that the interval function is always running, and if I press a button, just at 2500ms for example, 2 images jump very quickly (because the entire project is a slide/carousel)

BTNRIGHT.addEventListener('click', function(){
    next();
});
BTNLEFT.addEventListener('click', function(){
    prev();
});

//MOVIMIENTO AUTOMÁTICO
setInterval(function(){
    next();
}, 3000);

Thanks

It would be good if you put up the link for the entire code like in REPL or Codepen, so that everyone could see how everything connects to what you have here. Is this one of the challenge in FCC or your own project?

You can create a function that calls clearInterval before calling setInterval and have it run every time the buttons are clicked.

2 Likes

[RESOLVED]

BTNRIGHT.addEventListener('click', function(){
        next();
        //reiniciar intervalo al clickar en btn
        clearInterval(autocarousel);
        autocarousel = setInterval(function(){
            next();
        }, 4000);
    });
    BTNLEFT.addEventListener('click', function(){
        prev();
        //reiniciar intervalo al clickar en btn
        clearInterval(autocarousel);
        autocarousel = setInterval(function(){
            next();
        }, 4000);
    });

    //MOVIMIENTO AUTOMÁTICO
    let autocarousel = setInterval(function(){
        next();
    }, 4000);