How To Do A Clear Interval In Javascript?
My Code: $(function() { $('a.startSlideshow').click(function() { startSlideShow = window.setInterval(function() { slideSwitch() }, 1000); }); $('a.
Solution 1:
Declare startSlideShow
before your first handler:
var startSlideShow = null;
And clear it later:
if (startSlideShow) {
window.clearInterval(startSlideShow);
startSlideShow = null;
}
Solution 2:
I believe you have a scope issue:
$(function() {
var startSlideShow;
$('a.startSlideshow').click(function() {
startSlideShow = window.setInterval(function() { slideSwitch() }, 1000);
});
$('a.next').click(function() {
if (startSlideShow) {
clearInterval(startSlideShow);
}
nextSlide();
});
});
Define the variable outside of the click functions.
Solution 3:
That is because you do no declare startSlideShow, so it doesn't exist until the start function is run. At that point, it becomes a global variable with a value.
You should include it in a closure and declare it with var.
Post a Comment for "How To Do A Clear Interval In Javascript?"