如何停止 jQuery 淡入/出循环,stop() 不起作用



我有以下星星闪烁效果的函数,它是动画序列的一部分。

  function fadeInOut() 
    {
    $('#stars').fadeIn(800, function () {
         $('#stars').fadeOut(800, function () {
                $('#stars').fadeIn(800, function () {
                    setTimeout(fadeInOut, 500);
                });
            });
        });
    setTimeout(fadeInOut, 12500);
    }

在动画结束时,我提供了一个重播它的选项。为此,我想阻止这种闪烁效应。我尝试了以下方法,但没有用。

  function replayAnim()
    { $('#stars').clearQueue().stop(true); animStart();}

有人可以指出我正确的方向吗?

谢谢!

您还需要清除计时器

var timer;
function fadeInOut() {
    $('#stars').fadeIn(800, function () {
        $('#stars').fadeOut(800, function () {
            $('#stars').fadeIn(800, function () {
                timer = setTimeout(fadeInOut, 500);
            });
        });
    });
    timer = setTimeout(fadeInOut, 12500);
}
function replayAnim() {
    clearTimeout(timer)
    $('#stars').clearQueue().stop(true);
    animStart();
}

最新更新