Skip to content

Commit 8008f46

Browse files
committed
Merge branch 'master' of github.com:jquery/web-learn-jquery-com
* 'master' of github.com:jquery/web-learn-jquery-com: How To: fix title as the colon does not make nanoc happy adding working with events part 2 and correcting note on part 1 adding working with events part 1 correcting md adding keyboard navigation post adding events, using delegate and undelegate adding plugin development pattern post, correcting entity encoding adding making a jq plugin customizable post adding editrequired markdown correction for soln list adding enabling the back button adding queue examples post from gnarf.net adding queue/dequeue post
2 parents dc13dff + 61ad8d7 commit 8008f46

9 files changed

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

0 commit comments

Comments
 (0)