I have copied and modified a W3schools code, and now it just bugs out. I have set an interval of 2000 (2 seconds) and it slides the images how it should, however when I click on the next or previous buttons, it just simply slides too fast, sometimes it slides 3 times under one second, or 1-2 slides without clicking on it. (Looks like it slides fast (after clicking) on some parts).
And after I click on the buttons several times it just slides insanely fast until I refresh the page, which remains working how it should as long as I don't click on any prev or next button.
Here is the HTML code
<div class="main-slideShow-div">
<div class="slideshow-container">
<div class="my-slides">
<img class="pic" src="images/animals/dog1.jpg">
</div>
<div class="my-slides">
<img class="pic" src="images/animals/dog2.jpg">
</div>
<div class="my-slides">
<img class="pic" src="images/animals/dog3.jpg">
</div>
<img class="prev" onclick="plusSlides(-1)" src="images/rightArrow.png" alt="picture">
<img class="next" onclick="plusSlides(1)" src="images/leftArrow.png" alt="picture">
<div class="slider-div" style="text-align:center">
<div class="line" onclick="slideIndicator(1)"></div>
<div class="line" onclick="slideIndicator(2)"></div>
<div class="line" onclick="slideIndicator(3)"></div>
</div>
</div>
</div>
<script src="javascript1.js"></script>
And here is the JavaScript code
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n)
{
showSlides(slideIndex += n);
}
// Thumbnail image controls
function slideIndicator(n)
{
showSlides(slideIndex = n);
}
function showSlides(n)
{
var i;
var slides = document.getElementsByClassName("my-slides");
var line = document.getElementsByClassName("line");
slideIndex++;
if (slideIndex > slides.length)
{
slideIndex = 1
}
if (n < 1)
{
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++)
{
slides[i].style.display = "none";
}
for (i = 0; i < line.length; i++)
{
line[i].className = line[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
line[slideIndex-1].className += " active";
setTimeout(showSlides, 2000);
}
I'm aware that it increments the slider too many times, but can't figure out where.
Sorry for asking about a trivial code, I guess it should be understandable for me that what the problem is, but i'm just started using javascript.
And just one more question: what is that showSlides
at the beginning? If it is a global variable than how can you call it as a function later on?
Any help would be appreciated! :D