There is a slider with a slide switch after a certain period of time. I made it so that when I clicked on the slide browse button, the timer was reset to zero and the counting started from 0. The problem is that each time you press the button, the slider speeds up. Here is the code.

var sliderImages = document.querySelectorAll(".sales-slaider"); var arrowLeft = document.querySelector(".arrow-left"); var arrowRight = document.querySelector(".arrow-right"); var current = 0; // Clear all images function reset() { for (var i = 0; i < sliderImages.length; i++) { sliderImages[i].style.display = "none"; } } // Init slider function initSlide() { myTimer = setInterval(function () { if (current === sliderImages.length - 1) { current = -1; } slideRight(); }, 4000); } function startSlide() { reset(); sliderImages[0].style.display = "block"; initSlide(); } // Show prev function slideLeft() { reset(); sliderImages[current - 1].style.display = "block"; current--; } // Show next function slideRight() { reset(); sliderImages[current + 1].style.display = "block"; current++; } // Left arrow click arrowLeft.addEventListener("click", function () { if (current === 0) { current = sliderImages.length; } slideLeft(); clearInterval(myTimer); initSlide(); }); // Right arrow click arrowRight.addEventListener("click", function () { if (current === sliderImages.length - 1) { current = -1; } slideRight(); initSlide(); }); startSlide(); 
  • Problem solved. arrowRight.addEventListener ("click", function () {if (current === sliderImages.length - 1) {current = -1;} slideRight (); initSlide (); Missed celarInterval (myTimer) - Duman Torbaev
  • 2
    if the problem is solved on its own, it may be worth removing the question, he is unlikely to benefit anyone. Or you can post an answer. - Alex
  • Left, maybe someone will come in handy - Duman Torbaev
  • Thanks for the advice, I will post the answer) - Duman Torbaev

1 answer 1

Problem solved.

 arrowRight.addEventListener("click", function () { if (current === sliderImages.length - 1) { current = -1; } slideRight(); initSlide(); 

Missed celarInterval(myTimer) between slideRight(); and initSlide();