Home » Jquery » jquery – setInterval on mouseenter and clear interval on mouseleave

jquery – setInterval on mouseenter and clear interval on mouseleave

Posted by: admin February 22, 2020 Leave a comment

Questions:

I’m using setInterval to create a diy slideshow that starts on mouseenter using this tutorial :
https://www.amideveloper.com/how-to-start-slideshow-on-hover-image-in-jquery/

It works fine, but I would like the slideshow to stop on mouseleave by using clearInterval.

I don’t know what I’m doing wrong has my Interval is not cleared and the slideshow won’t stop…

here is my code :

JQUERY

$(".fadeInOut > div:gt(0)").hide();

function change_div() {

  $(".fadeInOut > div:first").fadeOut(0).next().fadeIn(0).end().appendTo(".fadeInOut");

}

$(".fadeInOut").mouseenter(function(){

    myVar = setInterval(change_div, 600);
  change_div();

});

$(".fadeInOut").mouseleave(function(){

  clearInterval(myVar);

});

HTML

<div class="fadeInOut">
    <div><img src="https://www.amideveloper.com/wp-content/themes/amideveloper/slide/slider-1.jpg"></div>
    <div><img src="https://www.amideveloper.com/wp-content/themes/amideveloper/slide/slider-2.jpg"></div>
    <div><img src="https://www.amideveloper.com/wp-content/themes/amideveloper/slide/slider-3.jpg"></div>
</div>

CSS

.fadeInOut > div {
    position: absolute;
}

here’s a link to a jsfidlle:

https://jsfiddle.net/0ysg3r67/

any help would be appreciated

How to&Answer:
var myVar;

$(".fadeInOut").mouseenter(function(){
  clearInterval(myVar);
  myVar = setInterval(change_div, 600);
  change_div();
});

Something with your slide transitions is causing the mouseenter to retrigger, creating multiple intervals. To safeguard against this, attempt to clear the interval before creating a new one.

As an alternative, instead of moving around slides, you can simply tag the one that is visible, and use that to know which slide should be shown next. This appears to resolve the issue with Firefox triggering the mouseenter over and over.

var myVar;
var $slides = $('.fadeInOut > div');

$(".fadeInOut > div:not(.current)").hide();

function change_div() {
	var $current = $slides.filter('.current');
  var $next = ($current.next().length ? $current.next() : $slides.first());
  
  $current.fadeOut(0).removeClass('current');
  $next.addClass('current').fadeIn(0);
}

$(".fadeInOut").mouseenter(function(){
	console.log('start');

	myVar = setInterval(change_div, 600);
  change_div();

});

$(".fadeInOut").mouseleave(function(){
  console.log('stop');
  clearInterval(myVar);

});
.fadeInOut > div {
    position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="fadeInOut">
	<div class="current"><img src="https://www.amideveloper.com/wp-content/themes/amideveloper/slide/slider-1.jpg"></div>
	<div><img src="https://www.amideveloper.com/wp-content/themes/amideveloper/slide/slider-2.jpg"></div>
	<div><img src="https://www.amideveloper.com/wp-content/themes/amideveloper/slide/slider-3.jpg"></div>
</div>