1 | --- ajax.js Fri Feb 09 09:56:03 2007 |
---|
2 | +++ ajax.modified.js Fri Feb 09 10:31:43 2007 |
---|
3 | @@ -611,8 +611,6 @@ |
---|
4 | if ( s.global && ! jQuery.active++ ) |
---|
5 | jQuery.event.trigger( "ajaxStart" ); |
---|
6 | |
---|
7 | - var requestDone = false; |
---|
8 | - |
---|
9 | // Create the request object |
---|
10 | var xml = new XMLHttpRequest(); |
---|
11 | |
---|
12 | @@ -642,91 +640,97 @@ |
---|
13 | if ( s.global ) |
---|
14 | jQuery.event.trigger("ajaxSend", [xml, s]); |
---|
15 | |
---|
16 | - // Wait for a response to come back |
---|
17 | - var onreadystatechange = function(isTimeout){ |
---|
18 | - // The transfer is complete and the data is available, or the request timed out |
---|
19 | - if ( xml && (xml.readyState == 4 || isTimeout == "timeout") ) { |
---|
20 | - requestDone = true; |
---|
21 | - var status; |
---|
22 | - try { |
---|
23 | - status = jQuery.httpSuccess( xml ) && isTimeout != "timeout" ? |
---|
24 | - s.ifModified && jQuery.httpNotModified( xml, s.url ) ? "notmodified" : "success" : "error"; |
---|
25 | - // Make sure that the request was successful or notmodified |
---|
26 | - if ( status != "error" ) { |
---|
27 | - // Cache Last-Modified header, if ifModified mode. |
---|
28 | - var modRes; |
---|
29 | - try { |
---|
30 | - modRes = xml.getResponseHeader("Last-Modified"); |
---|
31 | - } catch(e) {} // swallow exception thrown by FF if header is not available |
---|
32 | - |
---|
33 | - if ( s.ifModified && modRes ) |
---|
34 | - jQuery.lastModified[s.url] = modRes; |
---|
35 | + // Start polling the request |
---|
36 | + var intervalLock = false, |
---|
37 | + intervalCounter = 0, |
---|
38 | + intervalMillisec = 100, |
---|
39 | + intervalId = window.setInterval( function() { |
---|
40 | + if ( !intervalLock ) { |
---|
41 | + // Lock to prevent more executions while processing |
---|
42 | + intervalLock = true; |
---|
43 | + |
---|
44 | + // Check if the request has timed out |
---|
45 | + intervalCounter += 1; |
---|
46 | + var isTimeout = false; |
---|
47 | + if (s.timeout > 0) { |
---|
48 | + if (s.timeout <= intervalCounter * intervalMillisec) { |
---|
49 | + isTimeout = true; |
---|
50 | + |
---|
51 | + // Cancel the request |
---|
52 | + if ( xml ) { |
---|
53 | + xml.abort(); |
---|
54 | + } |
---|
55 | + } |
---|
56 | + } |
---|
57 | + |
---|
58 | + // The transfer is complete and the data is available, or the request timed out |
---|
59 | + if ( xml && (xml.readyState == 4 || isTimeout) ) { |
---|
60 | + // Clear interval |
---|
61 | + window.clearInterval( intervalId ); |
---|
62 | + |
---|
63 | + var status; |
---|
64 | + try { |
---|
65 | + status = jQuery.httpSuccess( xml ) && !isTimeout ? |
---|
66 | + s.ifModified && jQuery.httpNotModified( xml, s.url ) ? "notmodified" : "success" : "error"; |
---|
67 | + // Make sure that the request was successful or notmodified |
---|
68 | + if ( status != "error" ) { |
---|
69 | + // Cache Last-Modified header, if ifModified mode. |
---|
70 | + var modRes; |
---|
71 | + try { |
---|
72 | + modRes = xml.getResponseHeader("Last-Modified"); |
---|
73 | + } catch(e) {} // swallow exception thrown by FF if header is not available |
---|
74 | + |
---|
75 | + if ( s.ifModified && modRes ) |
---|
76 | + jQuery.lastModified[s.url] = modRes; |
---|
77 | + |
---|
78 | + // process the data (runs the xml through httpData regardless of callback) |
---|
79 | + var data = jQuery.httpData( xml, s.dataType ); |
---|
80 | + |
---|
81 | + // If a local callback was specified, fire it and pass it the data |
---|
82 | + if ( s.success ) |
---|
83 | + s.success( data, status ); |
---|
84 | + |
---|
85 | + // Fire the global callback |
---|
86 | + if( s.global ) |
---|
87 | + jQuery.event.trigger( "ajaxSuccess", [xml, s] ); |
---|
88 | + } else |
---|
89 | + jQuery.handleError(s, xml, status); |
---|
90 | + } catch(e) { |
---|
91 | + status = "error"; |
---|
92 | + jQuery.handleError(s, xml, status, e); |
---|
93 | + } |
---|
94 | |
---|
95 | - // process the data (runs the xml through httpData regardless of callback) |
---|
96 | - var data = jQuery.httpData( xml, s.dataType ); |
---|
97 | + // The request was completed |
---|
98 | + if( s.global ) |
---|
99 | + jQuery.event.trigger( "ajaxComplete", [xml, s] ); |
---|
100 | |
---|
101 | - // If a local callback was specified, fire it and pass it the data |
---|
102 | - if ( s.success ) |
---|
103 | - s.success( data, status ); |
---|
104 | + // Handle the global AJAX counter |
---|
105 | + if ( s.global && ! --jQuery.active ) |
---|
106 | + jQuery.event.trigger( "ajaxStop" ); |
---|
107 | |
---|
108 | - // Fire the global callback |
---|
109 | - if( s.global ) |
---|
110 | - jQuery.event.trigger( "ajaxSuccess", [xml, s] ); |
---|
111 | - } else |
---|
112 | - jQuery.handleError(s, xml, status); |
---|
113 | - } catch(e) { |
---|
114 | - status = "error"; |
---|
115 | - jQuery.handleError(s, xml, status, e); |
---|
116 | + // Process result |
---|
117 | + if ( s.complete ) |
---|
118 | + s.complete(xml, status); |
---|
119 | } |
---|
120 | + |
---|
121 | + // Clear the interval if the request is nullified |
---|
122 | + if ( !xml ) |
---|
123 | + windows.clearInterval( intervalId ); |
---|
124 | |
---|
125 | - // The request was completed |
---|
126 | - if( s.global ) |
---|
127 | - jQuery.event.trigger( "ajaxComplete", [xml, s] ); |
---|
128 | - |
---|
129 | - // Handle the global AJAX counter |
---|
130 | - if ( s.global && ! --jQuery.active ) |
---|
131 | - jQuery.event.trigger( "ajaxStop" ); |
---|
132 | - |
---|
133 | - // Process result |
---|
134 | - if ( s.complete ) |
---|
135 | - s.complete(xml, status); |
---|
136 | - |
---|
137 | - // Stop memory leaks |
---|
138 | - xml.onreadystatechange = function(){}; |
---|
139 | - xml = null; |
---|
140 | + // Remove lock |
---|
141 | + intervalLock = false; |
---|
142 | } |
---|
143 | - }; |
---|
144 | - xml.onreadystatechange = onreadystatechange; |
---|
145 | - |
---|
146 | - // Timeout checker |
---|
147 | - if ( s.timeout > 0 ) |
---|
148 | - setTimeout(function(){ |
---|
149 | - // Check to see if the request is still happening |
---|
150 | - if ( xml ) { |
---|
151 | - // Cancel the request |
---|
152 | - xml.abort(); |
---|
153 | - |
---|
154 | - if( !requestDone ) |
---|
155 | - onreadystatechange( "timeout" ); |
---|
156 | - } |
---|
157 | - }, s.timeout); |
---|
158 | - |
---|
159 | - // save non-leaking reference |
---|
160 | - var xml2 = xml; |
---|
161 | + }, intervalMillisec); |
---|
162 | |
---|
163 | // Send the data |
---|
164 | try { |
---|
165 | - xml2.send(s.data); |
---|
166 | + xml.send(s.data); |
---|
167 | } catch(e) { |
---|
168 | jQuery.handleError(s, xml, null, e); |
---|
169 | } |
---|
170 | - |
---|
171 | - // firefox 1.5 doesn't fire statechange for sync requests |
---|
172 | - if ( !s.async ) |
---|
173 | - onreadystatechange(); |
---|
174 | - |
---|
175 | + |
---|
176 | // return XMLHttpRequest to allow aborting the request etc. |
---|
177 | - return xml2; |
---|
178 | + return xml; |
---|
179 | }, |
---|
180 | |
---|
181 | handleError: function( s, xml, status, e ) { |
---|