0

I have this simple slider, with three slides, "previous" and "next" -buttons and dots which indicate which slide is currently showing. With some help, I have also managed to set "setInterval" -function to the slider so every slide plays after 2 seconds.

Now, after a lot of effort I still cannot figure out how to clear the interval when user clicks one of the buttons or dots.

I would be very thankful if someone could help me with this. You can find the code below.

var interval 
window.setInterval(function(){
    plusSlides(1);
}, 2000);

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}
                    
function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > 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 < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
}
.logocontainer {
  width: 800px;
  height: 100px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: lightblue;
}

.maincontainer {
  width: 800px;
  height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.dot-container {
  width: 800px;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  background-color: #fff;
  border-radius: 50%;
  margin: 5px;
}

.active, .dot:hover {
  background-color: black;
}

.ctacontainer {
  width: 800px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.ctacontainer a {
  text-decoration: none;
  background-color: orange;
  padding: 10px 20px 10px 20px;
  border-radius: 3px;
  color: white;
}

.containeritems {
  width: 400px;
  height: 150px;
  display: flex;
  position: relative;
  justify-content: center;
  align-items: center;
}

.mySlides div:nth-child(n) {
  animation-name: fader;
  animation-duration: 1s;
  z-index: 20;
}

@keyframes fader {
  from { opacity: 0.0; }
  to { opacity: 1.0; }
}

.prev, .next {
  cursor: pointer;
  padding: 12px;
  user-select: none;
  background-color: #000;
  color: white;
  margin: 5px;
  border-radius: 3px;
}

.image1, .image2, .image3 {
  display: flex;
  justify-content: center;
  align-items: center; /* OR FLEX-END */
  position: relative;
  color: white;
  width: 700px;
  height: 150px;
}

.image1 {
  background-color: lightgreen;
}

.image2 {
  background-color: lightgrey;
}

.image3 {
  background-color: red;
}
<head>
  <link rel = "stylesheet"
   type = "text/css"
   href = "Simpleslider.css" />
  
<meta name="viewport" content="width=800, initial-scale=1">
  
</head>

<div class="logocontainer">
  <h1>Logo</h1>
</div>

<div class="maincontainer">
  
  <div class="containeritems">
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    
    <div class="mySlides">
      <div class="image1"><h2>Image 1</h2></div>
     </div>
    <div class="mySlides">
      <div class="image2"><h2>Image 2</h2></div>
     </div>
    <div class="mySlides">
      <div class="image3"><h2>Image 3</h2></div>
     </div>
    
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
    
  </div>
  
</div>

  <div class="dot-container">
  
  <span class="dot" onclick="currentSlide(1)"></span>
  <span class="dot" onclick="currentSlide(2)"></span>
  <span class="dot" onclick="currentSlide(3)"></span>
  
</div>

<div class="ctacontainer">
  <a href="#">Click here</a>
</div>

<!-- INCLUDE JAVASCRIPT-->

<script type="text/javascript" src="Simpleslider.js"></script>
apple apple
  • 10,292
  • 2
  • 16
  • 36
Tane
  • 481
  • 2
  • 9
  • 20
  • 2
    `clearInterval()`? – apple apple Dec 18 '18 at 08:36
  • Thank you for your answer. Unfortunately it is not enough for me to solve the problem. Where I should insert the clearInterval() and is it the only thing I need? – Tane Dec 18 '18 at 08:42
  • https://stackoverflow.com/a/109091/5980430 would be a better dupe. since this Q has no `ajax` and `jQuery` – apple apple Dec 18 '18 at 08:44
  • Managed to solve this issue on my own. Added second class to onclick -function and applied clearInterval(intervalname) to this class. Works like a charm! Thanks for the help anyway. – Tane Dec 18 '18 at 09:44

0 Answers0