Skip to content

Commit

Permalink
Experiment switching to using onreadystatechange rather than a setInt…
Browse files Browse the repository at this point in the history
…erval for Ajax requests. Fixes #5735.
  • Loading branch information
jeresig committed Dec 31, 2009
1 parent a00e63e commit fe6c86d
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions src/ajax.js
Expand Up @@ -380,29 +380,21 @@ jQuery.extend({
}

// Wait for a response to come back
var onreadystatechange = function( isTimeout ) {
var onreadystatechange = xhr.onreadystatechange = function( isTimeout ) {
// The request was aborted, clear the interval and decrement jQuery.active
if ( !xhr || xhr.readyState === 0 ) {
if ( ival ) {
// clear poll interval
clearInterval( ival );
ival = null;

// Handle the global AJAX counter
if ( s.global && ! --jQuery.active ) {
jQuery.event.trigger( "ajaxStop" );
}
requestDone = true;
xhr.onreadystatechange = function(){};

// Handle the global AJAX counter
if ( s.global && ! --jQuery.active ) {
jQuery.event.trigger( "ajaxStop" );
}

// The transfer is complete and the data is available, or the request timed out
} else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
requestDone = true;

// clear poll interval
if (ival) {
clearInterval(ival);
ival = null;
}
xhr.onreadystatechange = function(){};

status = isTimeout === "timeout" ?
"timeout" :
Expand Down Expand Up @@ -446,19 +438,14 @@ jQuery.extend({
}
};

if ( s.async ) {
// don't attach the handler to the request, just poll it instead
var ival = setInterval(onreadystatechange, 13);

// Timeout checker
if ( s.timeout > 0 ) {
setTimeout(function() {
// Check to see if the request is still happening
if ( xhr && !requestDone ) {
onreadystatechange( "timeout" );
}
}, s.timeout);
}
// Timeout checker
if ( s.async && s.timeout > 0 ) {
setTimeout(function() {
// Check to see if the request is still happening
if ( xhr && !requestDone ) {
onreadystatechange( "timeout" );
}
}, s.timeout);
}

// Send the data
Expand Down

0 comments on commit fe6c86d

Please sign in to comment.