After attempting to create an automated slideshow with clickable buttons, I encountered a problem. The slideshow functions properly, but as soon as I click one of the buttons, the slideshow speeds up significantly.
This is what I implemented in my attempt:
<script>
var slideIndex = 0;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("button");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) { slideIndex = 1 }
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " active";
setTimeout(showSlides, 5000); // Change image every 10 seconds
}
</script>
Can anyone explain why the slideshow becomes erratic when clicking on the buttons for specific slides?
Below is the HTML code used:
<div class="wrapper">
<span class="button button1" onclick="currentSlide(0)"></span>
<span class="button button2" onclick="currentSlide(1)"></span>
<span class="button button3" onclick="currentSlide(2)"></span>
</div>
<!--Slideshow images-->
<div class="slideshow-container">
<div class="mySlides Fade">
<img src="homeScreen/teslaRoadster.jpg" style="width:100%">
<div class="centered-text"> Customize Your Own Car</div>
<button class="btnCustom">
<a id="costumLink" href="#custom">Customize</a>
</button>
</div>
<div class="mySlides Fade">
<img src="homeScreen/modelXHomeScreen.jpg" style="width:100%">
<div class="top-left"> MODEL X</div>
<button class="btnModelX">
<a id="costumLink" href="#modelx">Order Now</a>
</button>
</div>
<div class="mySlides Fade">
<img src="homeScreen/cybertruckHomeScreen.jpg" style="width:100%">
<div class="centered-text">The Future Is Now</div>
<button class="btnCyberTruck">
<a id="costumLink" href="#cybertruck">Order Now</a>
</button>
</div>
</div>