Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I add timer to carousel sliders using Javascript

I have a carousel sliders with text. I have my own custom js to those sliders. The carousel arrows are working fine but I want the sliders to move by itself after an interval.

Here's the HTML

<div class="col-6 col-xs-12  " style="text-align:-webkit-center;">  

 <div class="slideshow-container">

<div class="mySlides w3-container w3-center w3-animate-right">
 <h2 class="font-size" id="f37" data-animation="fadeInUp" data-delay="200ms">1</h2>

</div>

<div class="mySlides w3-container w3-center w3-animate-right">
 <h2 class="font-size" id="f37" data-animation="fadeInUp" data-delay="200ms">2</h2>

</div>

<div class="mySlides w3-container w3-center w3-animate-right">
 <h2 class="font-size" id="f37" data-animation="fadeInUp" data-delay="200ms">3</h2>

</div>

<a class="prev" onclick="plusSlides(-1)">&#10094;</a> 
<a class="next" onclick="plusSlides(1)">&#10095;</a>

   </div>
</div>

Here's the JS

<script>
  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";
            }
</script>

Hope I'm clear with the question. Ill highly appreciate your answer. Ive updated the code to codepen and you can observe the weird behavior it has once I click on the arrows although the auto slides are working fine when idle. https://codepen.io/mahirq8/pen/JjjJzbe

Thank you

like image 476
Muhammed Mahir Avatar asked Mar 04 '26 23:03

Muhammed Mahir


1 Answers

You would use setTimeout for that.

Add a variable timer at the top of your script:

var timer;

In your function showSlides, add the following two lines (anywhere, like at the end of it):

clearTimeout(timer);
timer = setTimeout(() => plusSlides(1), 2000);

If you now still use the arrow-buttons, the timer will reset and continue sliding the slides again 2 seconds after your last interference.

You can run it here:

var timer;
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";
  }
  slides[slideIndex - 1].style.display = "block";
  clearTimeout(timer);
  timer = setTimeout(() => plusSlides(1), 2000);
}
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: red;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

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

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .prev, .next,.text {font-size: 11px}
}
<div class="slideshow-container">

  <div class="mySlides fade">
    <h1>111</h1>
    <div class="text">Caption Text</div>
  </div>
  <div class="mySlides fade">
    <h1>222</h1>
    <div class="text">Caption Two</div>
  </div>
  <div class="mySlides fade">
    <h1>333</h1>
    <div class="text">Caption Three</div>
  </div>
  <div class="mySlides fade">
    <h1>444</h1>
    <div class="text">Caption Three</div>
  </div>
  <div class="mySlides fade">
    <h1>555</h1>
    <div class="text">Caption Three</div>
  </div>
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
like image 68
trincot Avatar answered Mar 06 '26 13:03

trincot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!