我希望滑块到达最后一张幻灯片时自动停止播放。
我已经尝试过这个:
if (currentSlide == 3) {
clearInterval(isPlaying);
}
但是这个没有用,知道怎么做吗?
这是我的代码:
var currentSlide = 0;
function showSlide() {
var slides = document.getElementsByClassName("slider__slide");
if (currentSlide >= slides.length) {
currentSlide = 0;
}
if (currentSlide < 0) {
currentSlide = 3;
}
for (i = 0; i < slides.length; i++) {
slides[i].classList.remove("slide--active");
}
slides[currentSlide].classList.add("slide--active");
}
var isPlaying = setInterval(function() { showSlide(currentSlide += 1); }, 8000);
function nextSlide() {
showSlide(currentSlide += 1);
clearInterval(isPlaying);
isPlaying = setInterval(function() { showSlide(currentSlide += 1); }, 8000);
}
function prevSlide() {
showSlide(currentSlide -= 1);
clearInterval(isPlaying);
isPlaying = setInterval(function() { showSlide(currentSlide += 1); }, 8000);
}