Shane Thompson Shane Thompson - 3 months ago 16
jQuery Question

Stop CSS & Jquery Slideshow from looping

I'm new to JQuery and don't know how to stop my jquery and css slideshow from looping. Here's all the syntax:

HTML:

<div id="slideshow">
<div>
<img src="Slide1.gif">
</div>
<div>
<img src="Slide2.gif">
</div>
<div>
<img src="Slide3.gif">
</div>
</div>


CSS:

#slideshow {
margin: 50px auto;
position: relative;
width: 755px;
height: 410px;
padding: 10px;
box-shadow: 0 0 20px rgba(0,0,0,0.4);
}

#slideshow > div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}


jQuery:

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

setInterval(function() {
$('#slideshow > div:first')
.fadeOut(2000)
.next()
.fadeIn(2000)
.end()
.appendTo('#slideshow');
}, 3000);


Can someone please selp me? Any would be greatly appreciated. Thank you in advance.

Answer

Declare a variable assigned to your interval function

var slideshowInterval = setInterval(function() { 
$('#slideshow > div:first')
.fadeOut(2000)
.next()
.fadeIn(2000)
.end()
.appendTo('#slideshow');
},  3000);

So when you want to stop it, just call the function clearInterval :

clearInterval(slideshowInterval);

http://www.w3schools.com/js/js_timing.asp