0

我查看了所有 clearInterval/clearTimeout 答案,他们都说要做我已经在做的事情。不过,我的超时和间隔仍不清楚。我在想可能是因为它在 Wordpress 中?IDK,请帮助...

jQuery(document).ready(function($){
var slideshow_timer;
var slideshow_delay;

    function initSlideshow() {
  slideshow_delay = setInterval(startSlideshow, 5000);
}

function startSlideshow() {
    if(!slideshow_timer){
        clearInterval(slideshow_delay);
        slideshow_timer = setTimeout(startSlideshow, 5000);
    }
}

function stopSlideshow() {
    if(slideshow_timer){
        clearTimeout(slideshow_timer);
    }else{  
        clearInterval(slideshow_delay);
    }
}
});

当我 console.log 变量时,我仍然得到数字。

*我删除了许多使幻灯片动画化的附加废话,并将其分解为间隔和超时。

4

2 回答 2

1

即使在清除计时器之后,您存储其返回值的变量也将保持不变。即使它有效,它也不会被设置回 0 或未定义。

于 2012-11-08T19:49:20.410 回答
1

Clearing the intervals does not reset the actual variables..

It just stops the timers.


If you need to restart the slideshow, then you will have to manually reset the variables once you stop the timers.

jQuery(document).ready(function($){
    var slideshow_timer;
    var slideshow_delay;

        function initSlideshow() {
      slideshow_delay = setInterval(startSlideshow, 5000);
    }

    function startSlideshow() {
        if(!slideshow_timer){
            clearInterval(slideshow_delay);
            slideshow_delay = null;
            slideshow_timer = setTimeout(startSlideshow, 5000);
        }
    }

    function stopSlideshow() {
        if(slideshow_timer){
            clearTimeout(slideshow_timer);
            slideshow_timer = null;
        }else{  
            clearInterval(slideshow_delay);
            slideshow_delay = null;
        }
    }
});
于 2012-11-08T19:50:18.590 回答