jQuery API

.queue()

Contents:

.queue( [queueName] ) Returns: Array

Description: Show the queue of functions to be executed on the matched elements.

  • version added: 1.2.queue( [queueName] )

    queueNameA string containing the name of the queue. Defaults to fx, the standard effects queue.

Example:

Show the length of the queue.

<!DOCTYPE html>
<html>
<head>
  <style>div { margin:3px; width:40px; height:40px;
        position:absolute; left:0px; top:60px; 
        background:green; display:none; }
  div.newcolor { background:blue; }
  p { color:red; }  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
  <p>The queue length is: <span></span></p>
  <div></div>
<script>
var div = $("div");

function runIt() {
  div.show("slow");
  div.animate({left:'+=200'},2000);
  div.slideToggle(1000);
  div.slideToggle("fast");
  div.animate({left:'-=200'},1500);
  div.hide("slow");
  div.show(1200);
  div.slideUp("normal", runIt);
}

function showIt() {
  var n = div.queue("fx");
  $("span").text( n.length );      
  setTimeout(showIt, 100);
}

runIt();
showIt();
</script>

</body>
</html>

Demo:

.queue( [queueName] , newQueue ) Returns: jQuery

Description: Manipulate the queue of functions to be executed on the matched elements.

  • version added: 1.2.queue( [queueName], newQueue )

    queueNameA string containing the name of the queue. Defaults to fx, the standard effects queue.

    newQueueAn array of functions to replace the current queue contents.

Every element can have one to many queues of functions attached to it by jQuery. In most applications, only one queue (called fx) is used. Queues allow a sequence of actions to be called on an element asynchronously, without halting program execution. The typical example of this is calling multiple animation methods on an element. For example:

$('#foo').slideUp().fadeIn();

When this statement is executed, the element begins its sliding animation immediately, but the fading transition is placed on the fx queue to be called only once the sliding transition is complete.

The .queue() method allows us to directly manipulate this queue of functions. Calling .queue() with a callback is particularly useful; it allows us to place a new function at the end of the queue.

This feature is similar to providing a callback function with an animation method, but does not require the callback to be given at the time the animation is performed.

$('#foo').slideUp();
$('#foo').queue(function() {
  alert('Animation complete.');
  $(this).dequeue();
});

This is equivalent to:

$('#foo').slideUp(function() {
  alert('Animation complete.');
});

Note that when adding a function with .queue(), we should ensure that .dequeue() is eventually called so that the next function in line executes.

In jQuery 1.4 the function that's called is passed in another function, as the first argument, that when called automatically dequeues the next item and keeps the queue moving. You would use it like so:

$("#test").queue(function(next) {
    // Do some stuff...
    next();
});

Examples:

Example: Queue a custom function.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { margin:3px; width:40px; height:40px;
        position:absolute; left:0px; top:30px; 
        background:green; display:none; }
  div.newcolor { background:blue; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  Click here...
  <div></div>
<script>$(document.body).click(function () {
      $("div").show("slow");
      $("div").animate({left:'+=200'},2000);
      $("div").queue(function () {
        $(this).addClass("newcolor");
        $(this).dequeue();
      });
      $("div").animate({left:'-=200'},500);
      $("div").queue(function () {
        $(this).removeClass("newcolor");
        $(this).dequeue();
      });
      $("div").slideUp();
    });</script>

</body>
</html>

Demo:

Example: Set a queue array to delete the queue.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { margin:3px; width:40px; height:40px;
        position:absolute; left:0px; top:30px; 
        background:green; display:none; }
  div.newcolor { background:blue; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="start">Start</button>
  <button id="stop">Stop</button>
  <div></div>
<script>$("#start").click(function () {
      $("div").show("slow");
      $("div").animate({left:'+=200'},5000);
      $("div").queue(function () {
        $(this).addClass("newcolor");
        $(this).dequeue();
      });
      $("div").animate({left:'-=200'},1500);
      $("div").queue(function () {
        $(this).removeClass("newcolor");
        $(this).dequeue();
      });
      $("div").slideUp();
    });
    $("#stop").click(function () {
      $("div").queue("fx", []);
      $("div").stop();
    });</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .queue() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .queue()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://twitter.com/jollytoad Mark Gibson

    It should be noted that the behaviour of the fn.queue with a custom queue name changed in jQuery 1.3+ … a function added to a named queue will not be called immediately if the queue was previously empty – as it would in earlier versions, or if added to the 'fx' queue.
    I have no idea why this change was made, but it really needs to be documented here.

  • Jørgen Johanson

    The first demo (“Show Length of Queue”) does not seem to work as it should in IE8 (the sideways movement is missing after clicking the button once). Works OK in FF3.6.3, Safari 4.0.5, Opera 10.53 and IE 7 compatilibity mode in IE8.

  • http://momentumworkshop.com Corey Frang

    There is a question on Stack Overflow that has some use cases and examples for learning what to do with .queue():

    http://stackoverflow.com/questions/1058158/can-…

  • bobo_mayday2007

    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    1
    1
    1
    1
    1
    1
    1
    1
    111
    1

    1

    1

    1
    111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111