From 0aa8d40cdfb50fd423a3bdcf585ac27f89df5b97 Mon Sep 17 00:00:00 2001 From: John Resig Date: Fri, 14 Nov 2008 04:16:00 +0000 Subject: [PATCH] Landing fixes for tickets #2935 and #2688 (Aborting ajax doesn't stop the setInterval, ajaxStop event isn't getting triggered). --- src/ajax.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index 7e024a319..4135e34c4 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -308,10 +308,11 @@ jQuery.extend({ s.accepts._default ); } catch(e){} - // Allow custom headers/mimetypes + // Allow custom headers/mimetypes and early abort if ( s.beforeSend && s.beforeSend(xhr, s) === false ) { - // cleanup active request counter - s.global && jQuery.active--; + // Handle the global AJAX counter + if ( s.global && ! --jQuery.active ) + jQuery.event.trigger( "ajaxStop" ); // close opended socket xhr.abort(); return false; @@ -322,8 +323,18 @@ jQuery.extend({ // Wait for a response to come back var onreadystatechange = function(isTimeout){ + // The request was aborted, clear the interval and decrement jQuery.active + if (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" ); + } // The transfer is complete and the data is available, or the request timed out - if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { + } else if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { requestDone = true; // clear poll interval