Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Trying to make a simple slideshow with setinterval() in Js

No onClick, just automatic change of image and caption every second. Here's the code I have so far: HTML:

<div class="col-md-6">
                <h2>Fishing Slide Show</h2>
                <h4 id="caption">Casting on the Upper Kings</h4>
                <img id="slide" src="images/casting1.jpg" alt="" height="200px">
                <div id="slides">
                    <img src="images/casting1.jpg" alt="Casting on the Upper Kings" height="200px">
                    <img src="images/casting2.jpg" alt="Casting on the Lower Kings" height="200px">
                    <img src="images/catchrelease.jpg" alt="Catch and Release on the Big Horn" height="200px">
                    <img src="images/fish.jpg" alt="Catching on the South Fork" height="200px">
                    <img src="images/lures.jpg" alt="The Lures for Catching" height="200px">
                </div>
            </div>

And this is my Javascript:

 var img_array = ["images/casting1.jpg", "images/casting2.jpg"
                    ,"images/catchrelease.jpg", "images/fish.jpg" 
                    ,"images/lures.jpg"];
   var alt_array = ["Casting on the Upper Kings", "Casting on the Lower Kings", 
                     "Catch and Release on the Big Horn", "Catching on the South Fork", 
                     "The Lures for Catching"];

   var currImg = 0;
   var currAlt = 0;

   function slideShow();{
 
         img.src = img_array[index++, img_array.length];       
         img.alt = alt_array[index++, alt_array.length];

       //document.getElementById("slides").src = img_array[currImg];
       //document.getElementById("slides").alt = alt_array[currAlt];
}
   let stop = setInterval(slideShow, 1000);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's a simple slideshow I made.

You simply have the first slide have the active tab, then you use .nextElementSibling to get the next slide.

function runSlideshow() {
  const current = document.querySelector(".slide.current");
  current.classList.toggle("current");
  current.nextElementSibling ?
    current.nextElementSibling.classList.toggle("current") :
    clearInterval(slideshow);
}

const slideshow = setInterval(runSlideshow, 2000);
.slide {
  height: 200px;
  display: none;
}

.slide.current {
  display: block;
}
<div id="container">
  <img class="slide current" src="https://cataas.com/cat/6136c137ebad510011b54b90" />
  <img class="slide" src="https://cataas.com/cat/6136be59ebad510011b54b88" />
  <img class="slide" src="https://cataas.com/cat/61307aa542fcca001bb841f5" />
  <img class="slide" src="https://cataas.com/cat/6123cb0fe8503700118bdb62" />
  <img class="slide" src="https://cataas.com/cat/6120c8103a342d0013920e23" />
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!