[英]make new interval when clicking on next or previous button on a slide
I have made an auto slide with interval of 8s, then i've added a previous and a next button that work but my problem now is that the interval is of course still running.我制作了一个间隔为 8 秒的自动幻灯片,然后我添加了一个可以工作的上一个和一个下一个按钮,但我现在的问题是间隔当然仍在运行。 Instead of that, I would like to make new interval each time I click on a button.取而代之的是,我想在每次单击按钮时设置新的间隔。 Could someone help me please?有人可以帮我吗? Thank you in advance:) have a good day提前谢谢你:) 祝你有美好的一天
javascript javascript
const prev = document.querySelector('.slider .fa-chevron-circle-left');
const next = document.querySelector('.slider .fa-chevron-circle-right');
setInterval(function() {
var active = document.querySelector('.show');
active.classList.remove('show');
if (active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev) {
active.nextElementSibling.classList.add('show');
} else {
active.parentElement.firstElementChild.classList.add('show');
}
}, 8000);
next.addEventListener('click', function() {
var nextSlide;
var active = document.querySelector('.show');
active.classList.remove('show');
nextSlide = active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev ? active.nextElementSibling.classList.add('show') : active.parentElement.firstElementChild.classList.add('show');
})
prev.addEventListener('click', function() {
var prevSlide;
var active = document.querySelector('.show');
active.classList.remove('show');
prevSlide = active.previousElementSibling && active.previousElementSibling !== next && active.previousElementSibling !== prev ? active.previousElementSibling.classList.add('show') : active.parentElement.children[2].classList.add('show');
})
html html
<div class="slider">
<div class="slide show"></div>
<div class="slide"></div>
<div class="slide"></div>
<i class="fas fa-chevron-circle-left"></i>
<i class="fas fa-chevron-circle-right"></i>
</div>
You should store the interval ID returned by setInterval
function, then clear it in event handlers:您应该存储setInterval
function 返回的间隔 ID,然后在事件处理程序中将其清除:
const intervalFunction = () => {
var active = document.querySelector('.show');
active.classList.remove('show');
if (active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev) {
active.nextElementSibling.classList.add('show');
} else {
active.parentElement.firstElementChild.classList.add('show');
}
}
let intervalId = setInterval(intervalFunction, 8000);
next.addEventListener('click', function() {
clearInterval(intervalId)
intervalId = setInterval(intervalFunction, 8000);
// ...
})
You can try this, I just clear the interval and restart the interval after the next/prev button clicked.你可以试试这个,我只是清除间隔并在单击下一个/上一个按钮后重新启动间隔。
function startSlider() {
return setInterval(function() {
var active = document.querySelector('.show');
active.classList.remove('show');
if (active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev) {
active.nextElementSibling.classList.add('show');
} else {
active.parentElement.firstElementChild.classList.add('show');
}
}, 8000)
}
var slideInterval = startSlider();
next.addEventListener('click', function() {
var nextSlide;
var active = document.querySelector('.show');
active.classList.remove('show');
nextSlide = active.nextElementSibling && active.nextElementSibling !== next && active.nextElementSibling !== prev ? active.nextElementSibling.classList.add('show') : active.parentElement.firstElementChild.classList.add('show');
clearInterval(slideInterval);
slideInterval = startSlider();
})
prev.addEventListener('click', function() {
var prevSlide;
var active = document.querySelector('.show');
active.classList.remove('show');
prevSlide = active.previousElementSibling && active.previousElementSibling !== next && active.previousElementSibling !== prev ? active.previousElementSibling.classList.add('show') : active.parentElement.children[2].classList.add('show');
clearInterval(slideInterval);
slideInterval = startSlider();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.