Skip to content

Commit fa479c1

Browse files
committed
adding queue examples post from gnarf.net
1 parent 0f9e7e0 commit fa479c1

File tree

2 files changed

+179
-1
lines changed

2 files changed

+179
-1
lines changed

content/effects/queue_and_dequeue_explained.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Queue & Dequeue Explained
33
attribution: Remy Sharp
4-
source: http://jqueryfordesigners.com/api-queue-dequeue/
4+
source: http://jqueryfordesigners.com/api-queue-dequeue/
55
---
66

77
When you use the animate and show, hide, slideUp, etc effect methods, you’re adding a job on to the fx queue.By default, using queue and passing a function, will add to the fx queue. So we’re creating our own bespoke animation step:
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: The uses of jQuery .queue() and .dequeue()
3+
attribution: Corey Frang
4+
source: http://gnarf.net/2010/09/30/the-uses-of-jquery-queue-and-dequeue/
5+
---
6+
7+
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().
8+
9+
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():
10+
11+
<div class="example" markdown="1">
12+
$.fn.delay = function( time, type ) {
13+
time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
14+
type = type || "fx";
15+
16+
return this.queue( type, function() {
17+
var elem = this;
18+
setTimeout(function() {
19+
jQuery.dequeue( elem, type );
20+
}, time );
21+
});
22+
};
23+
</div>
24+
25+
## The default queue – fx
26+
27+
The default queue in jQuery is fx. The default queue has some special properties that are not shared with other queues.
28+
29+
- 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.
30+
- ‘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.
31+
- It’s the default! The fx queue is used by .animate() and all functions that call it by default.
32+
33+
<div class="note">
34+
If you are using a custom queue, you must manually .dequeue() the functions, they will not auto start!
35+
Retrieving/Setting the queue
36+
</div>
37+
38+
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.
39+
40+
## Quick Examples:
41+
42+
<div class="example" markdown="1">
43+
// lets assume $elem is a jQuery object that points to some element we are animating.
44+
var queue = $elem.queue();
45+
// remove the last function from the animation queue.
46+
var lastFunc = queue.pop();
47+
// insert it at the beginning:
48+
queue.unshift(lastFunc);
49+
// replace queue with the first three items in the queue
50+
$elem.queue(queue.slice(0,3));
51+
</div>
52+
53+
### An animation (fx) queue example:
54+
55+
<div class="example" markdown="1">
56+
$(function() {
57+
// lets do something with google maps:
58+
var $map = $("#map_canvas");
59+
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
60+
var myOptions = {zoom: 8, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP};
61+
var geocoder = new google.maps.Geocoder();
62+
var map = new google.maps.Map($map[0], myOptions);
63+
var resized = function() {
64+
// simple animation callback - let maps know we resized
65+
google.maps.event.trigger(map, 'resize');
66+
};
67+
68+
// wait 2 seconds
69+
$map.delay(2000);
70+
// resize the div:
71+
$map.animate({
72+
width: 250,
73+
height: 250,
74+
marginLeft: 250,
75+
marginTop:250
76+
}, resized);
77+
// geocode something
78+
$map.queue(function(next) {
79+
// find stackoverflow's whois address:
80+
geocoder.geocode({'address': '55 Broadway New York NY 10006'},handleResponse);
81+
82+
function handleResponse(results, status) {
83+
if (status == google.maps.GeocoderStatus.OK) {
84+
var location = results[0].geometry.location;
85+
map.setZoom(13);
86+
map.setCenter(location);
87+
new google.maps.Marker({ map: map, position: location });
88+
}
89+
// geocoder result returned, continue with animations:
90+
next();
91+
}
92+
});
93+
// after we find stack overflow, wait 3 more seconds
94+
$map.delay(3000);
95+
// and resize the map again
96+
$map.animate({
97+
width: 500,
98+
height: 500,
99+
marginLeft:0,
100+
marginTop: 0
101+
}, resized);
102+
});
103+
</div>
104+
105+
### Queueing something like Ajax Calls:
106+
107+
108+
<div class="example" markdown="1">
109+
// jQuery on an empty object, we are going to use this as our Queue
110+
var ajaxQueue = $({});
111+
112+
$.ajaxQueue = function(ajaxOpts) {
113+
// hold the original complete function
114+
var oldComplete = ajaxOpts.complete;
115+
116+
// queue our ajax request
117+
ajaxQueue.queue(function(next) {
118+
119+
// create a complete callback to fire the next event in the queue
120+
ajaxOpts.complete = function() {
121+
// fire the original complete if it was there
122+
if (oldComplete) oldComplete.apply(this, arguments);
123+
124+
next(); // run the next query in the queue
125+
};
126+
127+
// run the query
128+
$.ajax(ajaxOpts);
129+
});
130+
};
131+
132+
// get each item we want to copy
133+
$("#items li").each(function(idx) {
134+
135+
// queue up an ajax request
136+
$.ajaxQueue({
137+
url: '/ajax_html_echo/',
138+
data: {html : "["+idx+"] "+$(this).html()},
139+
type: 'POST',
140+
success: function(data) {
141+
// Write to #output
142+
$("#output").append($("<li>", { html: data }));
143+
}
144+
});
145+
});
146+
</div>
147+
148+
### Another custom queue example
149+
150+
<div class="example" markdown="1">
151+
var theQueue = $({}); // jQuery on an empty object - a perfect queue holder
152+
153+
$.each([1,2,3],function(i, num) {
154+
// lets add some really simple functions to a queue:
155+
theQueue.queue('alerts', function(next) {
156+
// show something, and if they hit "yes", run the next function.
157+
if (confirm('index:'+i+' = '+num+'\nRun the next function?')) {
158+
next();
159+
}
160+
});
161+
});
162+
163+
// create a button to run the queue:
164+
$("<button>", {
165+
text: 'Run Queue',
166+
click: function() {
167+
theQueue.dequeue('alerts');
168+
}
169+
}).appendTo('body');
170+
171+
// create a button to show the length:
172+
$("<button>", {
173+
text: 'Show Length',
174+
click: function() {
175+
alert(theQueue.queue('alerts').length);
176+
}
177+
}).appendTo('body');
178+
</div>

0 commit comments

Comments
 (0)