@@ -16,23 +16,14 @@ queue function I've seen is `.delay()`:
16
16
17
17
```
18
18
$.fn.delay = function( time, type ) {
19
-
20
19
time = jQuery.fx ? jQuery.fx.speeds[ time ] || time : time;
21
-
22
20
type = type || "fx";
23
-
24
21
return this.queue( type, function() {
25
-
26
22
var elem = this;
27
-
28
23
setTimeout(function() {
29
-
30
24
jQuery.dequeue( elem, type );
31
-
32
25
}, time );
33
-
34
26
});
35
-
36
27
};
37
28
```
38
29
@@ -63,56 +54,60 @@ function.
63
54
## Quick Examples:
64
55
65
56
```
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
67
58
var queue = elem.queue();
68
59
69
- // Remove the last function from the animation queue.
60
+ // Remove the last function from the animation queue
70
61
var lastFunc = queue.pop();
71
62
72
- // Insert it at the beginning:
63
+ // Insert it at the beginning
73
64
queue.unshift( lastFunc );
74
65
75
- // Replace queue with the first three items in the queue.
66
+ // Replace queue with the first three items in the queue
76
67
elem.queue( queue.slice( 0, 3 ) );
77
68
```
78
69
79
70
### An animation (fx) queue example:
80
71
81
72
```
82
73
$(function() {
83
- // Let's do something with Google Maps:
74
+
75
+ // Let's do something with Google Maps
84
76
var canvas = $( "#map_canvas" );
85
77
86
- var myLatlng = new google.maps.LatLng( -34.397, 150.644 );
78
+ var latlng = new google.maps.LatLng( -34.397, 150.644 );
87
79
88
- var myOptions = {
80
+ var options = {
89
81
zoom: 8,
90
- center: myLatlng ,
82
+ center: latlng ,
91
83
mapTypeId: google.maps.MapTypeId.ROADMAP
92
84
};
93
85
94
86
var geocoder = new google.maps.Geocoder();
95
87
96
- var map = new google.maps.Map( canvas[0], myOptions );
88
+ var map = new google.maps.Map( canvas[0], options );
97
89
98
90
var resized = function() {
99
- // simple animation callback - let maps know we resized
91
+
92
+ // Simple animation callback - let maps know we resized
100
93
google.maps.event.trigger( map, "resize" );
101
94
};
102
95
103
- canvas.delay( 2000 ); // Wait for two seconds.
96
+ // Wait for two seconds
97
+ canvas.delay( 2000 );
104
98
105
- // resize the div:
99
+ // Resize the div
106
100
canvas.animate({
107
101
width: 250,
108
102
height: 250,
109
103
marginLeft: 250,
110
104
marginTop:250
111
105
}, resized );
112
106
113
- // geocode something
107
+ // Geocode something
114
108
canvas.queue(function( next ) {
115
- // find stackoverflow's whois address:
109
+
110
+ // Find Stack Overflow's whois address
116
111
geocoder.geocode( {
117
112
address: "55 Broadway New York NY 10006"
118
113
}, handleResponse );
@@ -128,15 +123,15 @@ $(function() {
128
123
});
129
124
}
130
125
131
- // geocoder result returned, continue with animations:
126
+ // Geocoder result returned, continue with animations
132
127
next();
133
128
}
134
129
});
135
130
136
- // after we find stack overflow , wait 3 more seconds
131
+ // After we find Stack Overflow , wait 3 more seconds
137
132
canvas.delay( 3000 );
138
133
139
- // and resize the map again
134
+ // Then resize the map again
140
135
canvas.animate({
141
136
width: 500,
142
137
height: 500,
@@ -153,37 +148,43 @@ $(function() {
153
148
var ajaxQueue = $({});
154
149
155
150
$.ajaxQueue = function( ajaxOpts ) {
156
- // Hold the original complete function.
151
+
152
+ // Hold the original complete function
157
153
var oldComplete = ajaxOpts.complete;
158
154
159
- // Queue our ajax request.
155
+ // Queue our ajax request
160
156
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
162
159
ajaxOpts.complete = function() {
163
- // Fire the original complete if it was there.
160
+
161
+ // Invoke the original complete if it was there
164
162
if ( oldComplete ) {
165
163
oldComplete.apply( this, arguments );
166
164
}
167
- // Run the next query in the queue.
165
+
166
+ // Run the next query in the queue
168
167
next();
169
168
};
170
169
171
- // Run the query.
170
+ // Run the query
172
171
$.ajax( ajaxOpts );
173
172
});
174
173
};
175
174
176
- // Get each item we want to copy.
175
+ // Get each item we want to copy
177
176
$( "#items li" ).each(function( idx ) {
178
- // Queue up an ajax request.
177
+
178
+ // Queue up an ajax request
179
179
$.ajaxQueue({
180
180
url: "/ajax_html_echo/",
181
181
data: {
182
182
html: "[" + idx + "] " + $( this ).html()
183
183
},
184
184
type: "POST",
185
185
success: function( data ) {
186
- // Write to #output.
186
+
187
+ // Write to #output
187
188
$( "#output" ).append( $( "<li>", {
188
189
html: data
189
190
}));
@@ -195,27 +196,30 @@ $( "#items li" ).each(function( idx ) {
195
196
### Another custom queue example
196
197
197
198
```
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 = $({});
199
201
200
202
$.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
202
205
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
204
208
if ( confirm( "index: " + i + " = " + num + "\nRun the next function?" ) ) {
205
209
next();
206
210
}
207
211
});
208
212
});
209
213
210
- // Create a button to run the queue.
214
+ // Create a button to run the queue
211
215
$( "<button>", {
212
216
text: "Run Queue",
213
217
click: function() {
214
218
theQueue.dequeue( "alerts" );
215
219
}
216
220
}).appendTo( "body" );
217
221
218
- // Create a button to show the length.
222
+ // Create a button to show the length
219
223
$( "<button>", {
220
224
text: "Show Length",
221
225
click: function() {
0 commit comments