1

i have created an automated slideshow which should change the picture after every 2 secs; i call the function changeSlide() at first then it should recursively call itself using the function setTimeout. but this doesn't work when page is first started or the page is refreshed.

but when i click on the button it starts working.

let slideIndex = 0;

    changeSlide();

    function changeSlide(){             
        slideIndex++;

        let images = document.getElementsByClassName('image');      
        for (let i = 0; i<images.length;i++){
            images[i].style.display="none";
        }

        if (slideIndex>= images.length){
            slideIndex = 0;
        }

        images[slideIndex].style.display = "block"; 
        setTimeout(changeSlide,2000);

    }

This is my html code.

<div class="w3-display-container w3-content">
        <img class="image" src="city.jpg" style="width: 100%;height:500px;" >
        <img class="image" src="profile.png" style="width: 100%; height:500px;display:none;" >
        <img class="image" src="img_girl.jpg" style="width: 100%;height:500px; display:none;" >

    </div>
    <button onClick="changeSlide()">Click Me</button>

i want my slideshow to start as soon as i start my page.

dead pool
  • 37
  • 8
  • change `images[slideIndex].style.display = "block";` to `if(images[slideIndex])images[slideIndex].style.display = "block";` or place your script tag after html. it seems your code executed before tags loaded – Zeeshan Anjum Apr 01 '19 at 19:36

3 Answers3

0

instead of changeSlide(); do window.onload=changeSlide; that way you make sure the DOM exists when calling the function

jonathan Heindl
  • 721
  • 4
  • 15
0

You currently seem to not have any code that tells your page to run the function when it's loaded. Try adding the following to your body tag:

<body onload="changeSlide()">
    <!-- your other HTML code here -->
</body>
  • `You currently seem to not have any code that tells your page to run the function when it's loaded ` he calls the function just on the line before he declares the function, not nice, but he calls it – caramba Apr 01 '19 at 19:38
  • Read over that. As others suggested, his code should work if the JS is placed after the HTML, though I'd recommend removing that line and using my solution so code placement isn't an issue –  Apr 01 '19 at 19:43
0

You should call changeSlide when browser has parsed your HTML markup, so it can access HTML elements. You have some options to make this work correctly.

  1. Place your JS code after your HTML markup (After the <div> element containing images). Or just call changeSlide after it:
<script>
    let slideIndex = 0;
    function changeSlide(){
        ...
    }
</script>

<div class="w3-display-container w3-content">
    ...
</div>

<script>
    changeSlide();
</script>
  1. Run changeSlide after page load using DOMContentLoaded event:
window.addEventListener('DOMContentLoaded', function(event){
    changeSlide();
});

// Or just:

window.addEventListener('DOMContentLoaded', changeSlide);