Skip to content

Commit 2e4bbb2

Browse files
committed
Uses of queue and dequeue: Cleanup
1 parent 4f961b7 commit 2e4bbb2

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

page/effects/uses-of-queue-and-dequeue.md

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,14 @@ queue function I've seen is `.delay()`:
1616

1717
```
1818
$.fn.delay = function( time, type ) {
19-
2019
time = jQuery.fx ? jQuery.fx.speeds[ time ] || time : time;
21-
2220
type = type || "fx";
23-
2421
return this.queue( type, function() {
25-
2622
var elem = this;
27-
2823
setTimeout(function() {
29-
3024
jQuery.dequeue( elem, type );
31-
3225
}, time );
33-
3426
});
35-
3627
};
3728
```
3829

@@ -63,56 +54,60 @@ function.
6354
## Quick Examples:
6455

6556
```
66-
// Let's assume elem is a jQuery object that points to some element we are animating.
57+
// Assume elem is a jQuery object that contains an element we are animating
6758
var queue = elem.queue();
6859
69-
// Remove the last function from the animation queue.
60+
// Remove the last function from the animation queue
7061
var lastFunc = queue.pop();
7162
72-
// Insert it at the beginning:
63+
// Insert it at the beginning
7364
queue.unshift( lastFunc );
7465
75-
// Replace queue with the first three items in the queue.
66+
// Replace queue with the first three items in the queue
7667
elem.queue( queue.slice( 0, 3 ) );
7768
```
7869

7970
### An animation (fx) queue example:
8071

8172
```
8273
$(function() {
83-
// Let's do something with Google Maps:
74+
75+
// Let's do something with Google Maps
8476
var canvas = $( "#map_canvas" );
8577
86-
var myLatlng = new google.maps.LatLng( -34.397, 150.644 );
78+
var latlng = new google.maps.LatLng( -34.397, 150.644 );
8779
88-
var myOptions = {
80+
var options = {
8981
zoom: 8,
90-
center: myLatlng,
82+
center: latlng,
9183
mapTypeId: google.maps.MapTypeId.ROADMAP
9284
};
9385
9486
var geocoder = new google.maps.Geocoder();
9587
96-
var map = new google.maps.Map( canvas[0], myOptions );
88+
var map = new google.maps.Map( canvas[0], options );
9789
9890
var resized = function() {
99-
// simple animation callback - let maps know we resized
91+
92+
// Simple animation callback - let maps know we resized
10093
google.maps.event.trigger( map, "resize" );
10194
};
10295
103-
canvas.delay( 2000 ); // Wait for two seconds.
96+
// Wait for two seconds
97+
canvas.delay( 2000 );
10498
105-
// resize the div:
99+
// Resize the div
106100
canvas.animate({
107101
width: 250,
108102
height: 250,
109103
marginLeft: 250,
110104
marginTop:250
111105
}, resized );
112106
113-
// geocode something
107+
// Geocode something
114108
canvas.queue(function( next ) {
115-
// find stackoverflow's whois address:
109+
110+
// Find Stack Overflow's whois address
116111
geocoder.geocode( {
117112
address: "55 Broadway New York NY 10006"
118113
}, handleResponse );
@@ -128,15 +123,15 @@ $(function() {
128123
});
129124
}
130125
131-
// geocoder result returned, continue with animations:
126+
// Geocoder result returned, continue with animations
132127
next();
133128
}
134129
});
135130
136-
// after we find stack overflow, wait 3 more seconds
131+
// After we find Stack Overflow, wait 3 more seconds
137132
canvas.delay( 3000 );
138133
139-
// and resize the map again
134+
// Then resize the map again
140135
canvas.animate({
141136
width: 500,
142137
height: 500,
@@ -153,37 +148,43 @@ $(function() {
153148
var ajaxQueue = $({});
154149
155150
$.ajaxQueue = function( ajaxOpts ) {
156-
// Hold the original complete function.
151+
152+
// Hold the original complete function
157153
var oldComplete = ajaxOpts.complete;
158154
159-
// Queue our ajax request.
155+
// Queue our ajax request
160156
ajaxQueue.queue(function( next ) {
161-
// Create a complete callback to fire the next event in the queue.
157+
158+
// Create a complete callback to invoke the next event in the queue
162159
ajaxOpts.complete = function() {
163-
// Fire the original complete if it was there.
160+
161+
// Invoke the original complete if it was there
164162
if ( oldComplete ) {
165163
oldComplete.apply( this, arguments );
166164
}
167-
// Run the next query in the queue.
165+
166+
// Run the next query in the queue
168167
next();
169168
};
170169
171-
// Run the query.
170+
// Run the query
172171
$.ajax( ajaxOpts );
173172
});
174173
};
175174
176-
// Get each item we want to copy.
175+
// Get each item we want to copy
177176
$( "#items li" ).each(function( idx ) {
178-
// Queue up an ajax request.
177+
178+
// Queue up an ajax request
179179
$.ajaxQueue({
180180
url: "/ajax_html_echo/",
181181
data: {
182182
html: "[" + idx + "] " + $( this ).html()
183183
},
184184
type: "POST",
185185
success: function( data ) {
186-
// Write to #output.
186+
187+
// Write to #output
187188
$( "#output" ).append( $( "<li>", {
188189
html: data
189190
}));
@@ -195,27 +196,30 @@ $( "#items li" ).each(function( idx ) {
195196
### Another custom queue example
196197

197198
```
198-
var theQueue = $({}); // jQuery on an empty object - a perfect queue holder
199+
// jQuery on an empty object - a perfect queue holder
200+
var theQueue = $({});
199201
200202
$.each([ 1, 2, 3 ], function( i, num ) {
201-
// Let's add some really simple functions to a queue:
203+
204+
// Add some really simple functions to a queue
202205
theQueue.queue( "alerts", function( next ) {
203-
// Show something, and if they hit "yes", run the next function.
206+
207+
// Show something, and if the user clicks "yes", run the next function
204208
if ( confirm( "index: " + i + " = " + num + "\nRun the next function?" ) ) {
205209
next();
206210
}
207211
});
208212
});
209213
210-
// Create a button to run the queue.
214+
// Create a button to run the queue
211215
$( "<button>", {
212216
text: "Run Queue",
213217
click: function() {
214218
theQueue.dequeue( "alerts" );
215219
}
216220
}).appendTo( "body" );
217221
218-
// Create a button to show the length.
222+
// Create a button to show the length
219223
$( "<button>", {
220224
text: "Show Length",
221225
click: function() {

0 commit comments

Comments
 (0)