ClearInterval不起作用

时间:2017-02-20 23:19:19

标签: javascript jquery clearinterval

有人可以解释为什么clearInterval电话无效吗?

 $(function() {
     var width = 720;
     var animationSpeed = 2000;
     var currentSlide = 1;
     var $slider = $("#slider");
     var $slideContainer = $slider.find('.slides');
     var $slides = $slideContainer.find('.slide')
     var intervalID = function() {
         if (currentSlide === $slides.length) {
             for (currentSlide = 1; currentSlide < $slides.length; currentSlide++) {
                 $slideContainer.animate({
                     "margin-left": '+=' + width
                 }, animationSpeed).delay(1000);
             }
         }
         if (currentSlide = 1) {
             for (currentSlide = 1; currentSlide < $slides.length; currentSlide++) {
                 $slideContainer.animate({
                     'margin-left': '-=' + width
                 }, animationSpeed).delay(1000);
             }
         }
     };

     function startSlider() {
         setInterval(intervalID);
     }

     function stopSlider() {
         clearInterval(intervalID);
     }
     $slider.on('mouseenter', stopSlider).on("mouseleave", startSlider);
     startSlider();
     `enter code here`
 });

1 个答案:

答案 0 :(得分:3)

您需要捕获maxiter的返回值并将其反馈给setInterval

clearInterval