Search code examples
javascriptjqueryhtmlslideshoww3.css

Slideshow Interval Not Reset After .mouseleave


I have created a simple-slideshow with W3.CSS's slideshow documentation. You can see the code and slideshow on this JS Fiddle.

Everything works except that the slideshow does not resume when on the "mouseleave" event.

$(document).ready(function(){
$("#slideshow").mouseenter(function () {
clearInterval(setInterval);})
.mouseleave(function(){setInterval(function(){
slideIndex++; currentDiv(slideIndex);}, 1000);});
});

I do not know how to reset the interval time with my code. Is there anyone willing to help me?

Here is my code in total:

<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<style>
.mySlides {display:none}
.w3-left, .w3-right, .w3-badge {cursor:pointer}
.w3-badge {height:13px;width:13px;padding:0}
</style>
<body>

<div class="w3-content w3-display-container" style="max-width:800px"  
id="slideshow" onmouseleave="showDivs()">
<div class="mySlides w3-container w3-xlarge w3-red w3-card-4">
<h1><b>Slide 1</b></h1>
</div>
<div class="mySlides w3-container w3-xlarge w3-blue w3-card-4" >
<p>Slide 2</p>
</div>
</div>
<div class="w3-center w3-container w3-section w3-large w3-text-white w3-
display-bottommiddle" style="width:100%">
<div class="w3-left w3-hover-text-khaki" onclick="plusDivs(-1)">&#10094;
</div>
<div class="w3-right w3-hover-text-khaki"onclick="plusDivs(1)">&#10095;
</div>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" 
onclick="currentDiv(1)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" 
onclick="currentDiv(2)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" 
onclick="currentDiv(3)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white" 
onclick="currentDiv(4)"></span>

<script>

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
showDivs(slideIndex += n);
}
function currentDiv(n) {
showDivs(slideIndex = n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
if (n > x.length) {slideIndex = 1}    
if (n < 1) {slideIndex = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";  
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-white", "");
}
x[slideIndex-1].style.display = "block";  
dots[slideIndex-1].className += " w3-white";
}
var setInterval = setInterval(function(){slideIndex++; 
currentDiv(slideIndex);}, 1000);

$(document).ready(function(){
$("#slideshow").mouseenter(function () {clearInterval(setInterval);})
.mouseleave(function(){setInterval(function(){slideIndex++; 
currentDiv(slideIndex);}, 1000);});
});
</script>

</body>
</html> 

Solution

  • You are not properly establishing a reference to your timer, so clearing it isn't working.

    Don't name a variable the name of a function, Object, keyword or method:

    var setInterval = setInterval(function(){slideIndex++; currentDiv(slideIndex);}, 1000);
    

    because it can override/disable the native functionality provided by that name.

    $(document).ready(function(){
      var timer = null; // This will hold a reference to the timer
    
      $("#slideshow").mouseenter(function () {
        clearInterval(timer); // It's the timer reference you need to clear
      })
      .mouseleave(function(){
        // Now, you have to establish the timer reference
        timer = setInterval(function(){
          slideIndex++; currentDiv(slideIndex);
        }, 1000);
      });
    });