javascript - Abort Ajax requêtes utilisant jQuery

Mots clés : javascriptjqueryajaxjavascript

meilleur 2 Réponses javascript - Abort Ajax requêtes utilisant jQuery

vote vote

96

var xhr = $.ajax({     type: "POST",     url: "some.php",     data: "name=John&location=Boston",     success: function(msg){        alert( "Data Saved: " + msg );     } });  //kill the request xhr.abort() 
vote vote

86

var xhrCount = 0; function sendXHR() {     // sequence number for the current invocation of function     var seqNumber = ++xhrCount;     $.post("/echo/json/", { delay: Math.floor(Math.random() * 5) }, function() {         // this works because of the way closures work         if (seqNumber === xhrCount) {             console.log("Process the response");         } else {             console.log("Ignore the response");         }     }); } sendXHR(); sendXHR(); sendXHR(); // AJAX requests complete in any order but only the last  // one will trigger "Process the response" message 

Questions similaires