Jquery:stop(true)和clearQueue()之间有什么区别

时间:2011-03-29 00:40:54

标签: jquery

使用stop()并将clearQueue参数设置为true并使用clearqueue()之间有什么区别?如果两者都没有差异,那么两者之间有什么区别?

2 个答案:

答案 0 :(得分:7)

在api文档中,.stop()仅适用于动画,但.clearqueue()将删除附加到标准jquery队列的任何函数。

来自docs

  

当.clearQueue()方法是   调用队列中的所有函数   尚未执行的是   从队列中删除。使用时   没有参数,.clearQueue()   从中删除剩余的功能   fx,标准效果队列。在   这种方式类似于.stop(true)。   但是,虽然.stop()方法是   仅用于动画,   .clearQueue()也可以用来   删除任何已经执行的功能   添加到通用jQuery队列中   .queue()方法。

答案 1 :(得分:1)

jQuery支持许多队列,其中最常见的是动画的fx队列。 .stop()仅适用于fx队列,而clearQueue则允许您指定其他(自定义)队列。


以下是自定义队列的示例:

// First function to queue
function a1(next) {
    setTimeout(function () {
        alert("one");
        next();
    }, 1000);
}

// Second function to queue
function a2(next) {
    setTimeout(function () {
        alert("two");
        next();
    }, 1000);
}

// Queue both functions and start it off
$('body').queue('alerts', a1).queue('alerts', a2).dequeue('alerts');

// Clear the queue to prevent the second alert from showing
$('body').clearQueue('alerts');

请参阅demo