]> source.dussan.org Git - jquery.git/commitdiff
Ajax: add an ontimeout handler to all requests
authorErik Lax <erik@datahack.se>
Thu, 23 Mar 2017 13:33:25 +0000 (14:33 +0100)
committerTimmy Willison <4timmywil@gmail.com>
Mon, 24 Jul 2017 15:44:09 +0000 (11:44 -0400)
Fixes gh-3586
Close gh-3590

src/ajax/xhr.js
test/unit/ajax.js

index 33dafb01d3429fc9f7d4353ca122273b692a6f30..4a31171aca113c0c629248f0fe14fdd26eebb8e3 100644 (file)
@@ -75,7 +75,8 @@ jQuery.ajaxTransport( function( options ) {
                                        return function() {
                                                if ( callback ) {
                                                        callback = errorCallback = xhr.onload =
-                                                               xhr.onerror = xhr.onabort = xhr.onreadystatechange = null;
+                                                               xhr.onerror = xhr.onabort = xhr.ontimeout =
+                                                                       xhr.onreadystatechange = null;
 
                                                        if ( type === "abort" ) {
                                                                xhr.abort();
@@ -115,7 +116,7 @@ jQuery.ajaxTransport( function( options ) {
 
                                // Listen to events
                                xhr.onload = callback();
-                               errorCallback = xhr.onerror = callback( "error" );
+                               errorCallback = xhr.onerror = xhr.ontimeout = callback( "error" );
 
                                // Support: IE 9 only
                                // Use onreadystatechange to replace onabort
index 07ff40783093041f041ba6afc85c30a5e0824732..4dc7b9d3a6f912a9758695e68fece36b41a1e446 100644 (file)
@@ -527,6 +527,23 @@ QUnit.module( "ajax", {
                };
        } );
 
+       ajaxTest( "jQuery.ajax() - native timeout", 2, function( assert ) {
+               return {
+                       url: url( "data/name.php?wait=1" ),
+                       xhr: function() {
+                               var xhr = new window.XMLHttpRequest();
+                               xhr.timeout = 1;
+                               return xhr;
+                       },
+                       error: function( xhr, msg ) {
+                               assert.strictEqual( msg, "error", "Native timeout triggers error callback" );
+                       },
+                       complete: function() {
+                               assert.ok( true, "complete" );
+                       }
+               };
+       } );
+
        ajaxTest( "jQuery.ajax() - events with context", 12, function( assert ) {
                var context = document.createElement( "div" );