Jquery: What is the difference between stop(true) and clearQueue()
What is the difference between using a stop()
with the clearQueue
parameter set to true and just using the cle开发者_开发知识库arqueue()
, and if there is no difference why both?
From the api docs, .stop()
is meant only for animation, however .clearqueue()
will remove any function attached to a standard jquery queue.
From the docs:
When the .clearQueue() method is called, all functions on the queue that have not been executed are removed from the queue. When used without an argument, .clearQueue() removes the remaining functions from fx, the standard effects queue. In this way it is similar to .stop(true). However, while the .stop() method is meant to be used only with animations, .clearQueue() can also be used to remove any function that has been added to a generic jQuery queue with the .queue() method.
jQuery supports a number of queues, the most common of which is the fx
queue for animations. .stop()
works only on the fx
queue, while clearQueue
lets you specify some other (custom) queue.
Here is an example with a custom queue:
// 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');
See the demo.
精彩评论