Detect JSON Ajax requests by the response content-type (like is done with XML). Fixes...
[jquery.git] / src / queue.js
1 jQuery.extend({
2         queue: function( elem, type, data ) {
3                 if ( !elem ) {
4                         return;
5                 }
6
7                 type = (type || "fx") + "queue";
8                 var q = jQuery.data( elem, type );
9
10                 // Speed up dequeue by getting out quickly if this is just a lookup
11                 if ( !data ) {
12                         return q || [];
13                 }
14
15                 if ( !q || jQuery.isArray(data) ) {
16                         q = jQuery.data( elem, type, jQuery.makeArray(data) );
17
18                 } else {
19                         q.push( data );
20                 }
21
22                 return q;
23         },
24
25         dequeue: function( elem, type ) {
26                 type = type || "fx";
27
28                 var queue = jQuery.queue( elem, type ), fn = queue.shift();
29
30                 // If the fx queue is dequeued, always remove the progress sentinel
31                 if ( fn === "inprogress" ) {
32                         fn = queue.shift();
33                 }
34
35                 if ( fn ) {
36                         // Add a progress sentinel to prevent the fx queue from being
37                         // automatically dequeued
38                         if ( type === "fx" ) {
39                                 queue.unshift("inprogress");
40                         }
41
42                         fn.call(elem, function() {
43                                 jQuery.dequeue(elem, type);
44                         });
45                 }
46         }
47 });
48
49 jQuery.fn.extend({
50         queue: function( type, data ) {
51                 if ( typeof type !== "string" ) {
52                         data = type;
53                         type = "fx";
54                 }
55
56                 if ( data === undefined ) {
57                         return jQuery.queue( this[0], type );
58                 }
59                 return this.each(function( i, elem ) {
60                         var queue = jQuery.queue( this, type, data );
61
62                         if ( type === "fx" && queue[0] !== "inprogress" ) {
63                                 jQuery.dequeue( this, type );
64                         }
65                 });
66         },
67         dequeue: function( type ) {
68                 return this.each(function() {
69                         jQuery.dequeue( this, type );
70                 });
71         },
72
73         // Based off of the plugin by Clint Helfers, with permission.
74         // http://blindsignals.com/index.php/2009/07/jquery-delay/
75         delay: function( time, type ) {
76                 time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
77                 type = type || "fx";
78
79                 return this.queue( type, function() {
80                         var elem = this;
81                         setTimeout(function() {
82                                 jQuery.dequeue( elem, type );
83                         }, time );
84                 });
85         },
86
87         clearQueue: function( type ) {
88                 return this.queue( type || "fx", [] );
89         }
90 });