--- ajax.js Fri Feb 09 09:56:03 2007 +++ ajax.modified.js Fri Feb 09 10:31:43 2007 @@ -611,8 +611,6 @@ if ( s.global && ! jQuery.active++ ) jQuery.event.trigger( "ajaxStart" ); - var requestDone = false; - // Create the request object var xml = new XMLHttpRequest(); @@ -642,91 +640,97 @@ if ( s.global ) jQuery.event.trigger("ajaxSend", [xml, s]); - // Wait for a response to come back - var onreadystatechange = function(isTimeout){ - // The transfer is complete and the data is available, or the request timed out - if ( xml && (xml.readyState == 4 || isTimeout == "timeout") ) { - requestDone = true; - var status; - try { - status = jQuery.httpSuccess( xml ) && isTimeout != "timeout" ? - s.ifModified && jQuery.httpNotModified( xml, s.url ) ? "notmodified" : "success" : "error"; - // Make sure that the request was successful or notmodified - if ( status != "error" ) { - // Cache Last-Modified header, if ifModified mode. - var modRes; - try { - modRes = xml.getResponseHeader("Last-Modified"); - } catch(e) {} // swallow exception thrown by FF if header is not available - - if ( s.ifModified && modRes ) - jQuery.lastModified[s.url] = modRes; + // Start polling the request + var intervalLock = false, + intervalCounter = 0, + intervalMillisec = 100, + intervalId = window.setInterval( function() { + if ( !intervalLock ) { + // Lock to prevent more executions while processing + intervalLock = true; + + // Check if the request has timed out + intervalCounter += 1; + var isTimeout = false; + if (s.timeout > 0) { + if (s.timeout <= intervalCounter * intervalMillisec) { + isTimeout = true; + + // Cancel the request + if ( xml ) { + xml.abort(); + } + } + } + + // The transfer is complete and the data is available, or the request timed out + if ( xml && (xml.readyState == 4 || isTimeout) ) { + // Clear interval + window.clearInterval( intervalId ); + + var status; + try { + status = jQuery.httpSuccess( xml ) && !isTimeout ? + s.ifModified && jQuery.httpNotModified( xml, s.url ) ? "notmodified" : "success" : "error"; + // Make sure that the request was successful or notmodified + if ( status != "error" ) { + // Cache Last-Modified header, if ifModified mode. + var modRes; + try { + modRes = xml.getResponseHeader("Last-Modified"); + } catch(e) {} // swallow exception thrown by FF if header is not available + + if ( s.ifModified && modRes ) + jQuery.lastModified[s.url] = modRes; + + // process the data (runs the xml through httpData regardless of callback) + var data = jQuery.httpData( xml, s.dataType ); + + // If a local callback was specified, fire it and pass it the data + if ( s.success ) + s.success( data, status ); + + // Fire the global callback + if( s.global ) + jQuery.event.trigger( "ajaxSuccess", [xml, s] ); + } else + jQuery.handleError(s, xml, status); + } catch(e) { + status = "error"; + jQuery.handleError(s, xml, status, e); + } - // process the data (runs the xml through httpData regardless of callback) - var data = jQuery.httpData( xml, s.dataType ); + // The request was completed + if( s.global ) + jQuery.event.trigger( "ajaxComplete", [xml, s] ); - // If a local callback was specified, fire it and pass it the data - if ( s.success ) - s.success( data, status ); + // Handle the global AJAX counter + if ( s.global && ! --jQuery.active ) + jQuery.event.trigger( "ajaxStop" ); - // Fire the global callback - if( s.global ) - jQuery.event.trigger( "ajaxSuccess", [xml, s] ); - } else - jQuery.handleError(s, xml, status); - } catch(e) { - status = "error"; - jQuery.handleError(s, xml, status, e); + // Process result + if ( s.complete ) + s.complete(xml, status); } + + // Clear the interval if the request is nullified + if ( !xml ) + windows.clearInterval( intervalId ); - // The request was completed - if( s.global ) - jQuery.event.trigger( "ajaxComplete", [xml, s] ); - - // Handle the global AJAX counter - if ( s.global && ! --jQuery.active ) - jQuery.event.trigger( "ajaxStop" ); - - // Process result - if ( s.complete ) - s.complete(xml, status); - - // Stop memory leaks - xml.onreadystatechange = function(){}; - xml = null; + // Remove lock + intervalLock = false; } - }; - xml.onreadystatechange = onreadystatechange; - - // Timeout checker - if ( s.timeout > 0 ) - setTimeout(function(){ - // Check to see if the request is still happening - if ( xml ) { - // Cancel the request - xml.abort(); - - if( !requestDone ) - onreadystatechange( "timeout" ); - } - }, s.timeout); - - // save non-leaking reference - var xml2 = xml; + }, intervalMillisec); // Send the data try { - xml2.send(s.data); + xml.send(s.data); } catch(e) { jQuery.handleError(s, xml, null, e); } - - // firefox 1.5 doesn't fire statechange for sync requests - if ( !s.async ) - onreadystatechange(); - + // return XMLHttpRequest to allow aborting the request etc. - return xml2; + return xml; }, handleError: function( s, xml, status, e ) {