Please see the code. I can't get the variable's value from this displayNextSlide() function. I want to have another variable to get the index's value after the recursion stop. I want tempIndex have index's value after the recursion.
var index = 0;
let tempIndex = 0;
var x = document.getElementsByClassName("mySlides");
function displayNextSlide() {
for (var i = 0; i < x.length; i++) {
if (i === index) {
x[i].style.display = "block";
} else {
x[i].style.display = "none";
}
}
index++;
if (index == x.length) {
clearInterval(interval);
}
}
displayNextSlide();
var interval = setInterval(displayNextSlide, 500);
console.log(index);
<!DOCTYPE html>
<html>
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {
display: none;
}
</style>
<body>
<h2 class="w3-center">Slideshow test</h2>
<div class="w3-content w3-section" style="max-width:500px">
<div class="mySlides" src="1.jpg" style="width:100%">1</div>
<div class="mySlides" src="2.jpg" style="width:100%">2</div>
<div class="mySlides" src="3.jpg" style="width:100%">3</div>
<div class="mySlides" src="4.jpg" style="width:100%">4</div>
<div class="mySlides" src="5.jpg" style="width:100%">5</div>
<div class="mySlides" src="6.jpg" style="width:100%">6</div>
<div class="mySlides" src="7.jpg" style="width:100%">7</div>
<div class="mySlides" src="8.jpg" style="width:100%">8</div>
<div class="mySlides" src="9.jpg" style="width:100%">9</div>
<div class="mySlides" src="10.jpg" style="width:100%">10</div>
</div>