The uses of jQuery .queue() and .dequeue()

jQuery

The uses of jQuery .queue() and .dequeue()

Queues in jQuery are used for animations. You can use them for any purpose you like. They are an array of functions stored on a per element basis, using jQuery.data(). The are First-In-First-Out (FIFO). You can add a function to the queue by calling .queue(), and you remove (by calling) the functions using .dequeue().

To understand the internal jQuery queue functions, reading the source and looking at examples helps me out tremendously. One of the best examples of a queue function I’ve seen is .delay():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
                          
$.fn.delay = function( time, type ) {
time = jQuery.fx ? jQuery.fx.speeds[ time ] || time : time;
type = type || "fx";
return this.queue( type, function() {
var elem = this;
setTimeout(function() {
jQuery.dequeue( elem, type );
}, time );
});
};

The default queue – fx

The default queue in jQuery is fx. The default queue has some special properties that are not shared with other queues.

  • Auto Start: When calling $(elem).queue(function() {}); the fx queue will automatically dequeue the next function and run it if the queue hasn’t started.
  • ‘inprogress’ sentinel: Whenever you dequeue() a function from the fx queue, it will unshift() (push into the first location of the array) the string "inprogress" – which flags that the queue is currently being run.
  • It’s the default! The fx queue is used by .animate() and all functions that call it by default.
If you are using a custom queue, you must manually .dequeue() the functions, they will not auto start!

Retrieving/Setting the queue

You can retrieve a reference to a jQuery queue by calling .queue() without a function argument. You can use the method if you want to see how many items are in the queue. You can use push, pop, unshift, shift to manipulate the queue in place. You can replace the entire queue by passing an array to the .queue() function.

Quick Examples:

1
2
3
4
5
6
7
8
9
10
11
                          
// lets assume $elem is a jQuery object that points to some element we are animating.
var queue = $elem.queue();
// remove the last function from the animation queue.
var lastFunc = queue.pop();
// insert it at the beginning:
queue.unshift( lastFunc );
// replace queue with the first three items in the queue
$elem.queue( queue.slice( 0, 3 ) );

An animation (fx) queue example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
                          
$(function() {
// lets do something with google maps:
var $map = $("#map_canvas");
var myLatlng = new google.maps.LatLng( -34.397, 150.644 );
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map( $map[0], myOptions );
var resized = function() {
// simple animation callback - let maps know we resized
google.maps.event.trigger( map, "resize" );
};
// wait 2 seconds
$map.delay( 2000 );
// resize the div:
$map.animate({
width: 250,
height: 250,
marginLeft: 250,
marginTop:250
}, resized );
// geocode something
$map.queue(function( next ) {
// find stackoverflow's whois address:
geocoder.geocode( {
address: "55 Broadway New York NY 10006"
}, handleResponse );
function handleResponse( results, status ) {
if ( status === google.maps.GeocoderStatus.OK ) {
var location = results[ 0 ].geometry.location;
map.setZoom( 13 );
map.setCenter( location );
new google.maps.Marker({
map: map,
position: location
});
}
// geocoder result returned, continue with animations:
next();
}
});
// after we find stack overflow, wait 3 more seconds
$map.delay( 3000 );
// and resize the map again
$map.animate({
width: 500,
height: 500,
marginLeft:0,
marginTop: 0
}, resized );
});

Queueing something like Ajax Calls:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
                          
// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = $({});
$.ajaxQueue = function( ajaxOpts ) {
// hold the original complete function
var oldComplete = ajaxOpts.complete;
// queue our ajax request
ajaxQueue.queue(function( next ) {
// create a complete callback to fire the next event in the queue
ajaxOpts.complete = function() {
// fire the original complete if it was there
if ( oldComplete ) {
oldComplete.apply( this, arguments );
}
// run the next query in the queue
next();
};
// run the query
$.ajax( ajaxOpts );
});
};
// get each item we want to copy
$("#items li").each(function( idx ) {
// queue up an ajax request
$.ajaxQueue({
url: "/ajax_html_echo/",
data: {
html : "[" + idx + "] " + $( this ).html()
},
type: "POST",
success: function( data ) {
// Write to #output
$("#output").append( $("<li>", {
html: data
}));
}
});
});

Another custom queue example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
                          
var theQueue = $({}); // jQuery on an empty object - a perfect queue holder
$.each( [1,2,3], function( i, num ) {
// lets add some really simple functions to a queue:
theQueue.queue( "alerts", function(next) {
// show something, and if they hit "yes", run the next function.
if ( confirm("index:" + i + " = " + num + "\nRun the next function?") ) {
next();
}
});
});
// create a button to run the queue:
$("<button>", {
text: "Run Queue",
click: function() {
theQueue.dequeue("alerts");
}
}).appendTo("body");
// create a button to show the length:
$("<button>", {
text: "Show Length",
click: function() {
alert(theQueue.queue("alerts").length);
}
}).appendTo("body");