Currently, I am working on implementing a swiper slider with a countdown feature that resets its value every time a slide change occurs. The countdown should restart when the next slide is displayed automatically. However, I have encountered an issue where clicking on the navigation dots results in a double countdown effect. It seems like the clearInterval function initiates another countdown without properly removing the previous countdowns from the thread execution.
var autoplay = 20000;
var swiper = new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
paginationClickable: true,
watchSlidesProgress: true,
autoplay: 20000,
onProgress: elipse
});
function elipse() {
clearInterval(timer);
var countdownNumberEl = document.getElementById('countdown-number');
var countdown = 20;
countdownNumberEl.textContent = countdown;
var timer = setInterval(frame, 1000);
function frame(){
countdown = --countdown <= 0 ? 20 : countdown;
countdownNumberEl.textContent = countdown;
}
}
var swiperBullet = document.getElementsByClassName("swiper-pagination-bullet");
for (var i = 0; i < swiperBullet.length; i++) {
swiperBullet[i].addEventListener('click', function () {
elipse();
});
}
I would appreciate any assistance in resolving this issue and understanding what is causing it.