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.