How to Stop All Ajax Process in the Page Using Jquery

Stop all active ajax requests in jQuery

Every time you create an ajax request you could use a variable to store it:

var request = $.ajax({
type: 'POST',
url: 'someurl',
success: function(result){}
});

Then you can abort the request:

request.abort();

You could use an array keeping track of all pending ajax requests and abort them if necessary.

Abort Ajax requests using jQuery

Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use abort().

See the documentation:

  • abort Method (MSDN). Cancels the current HTTP request.
  • abort() (MDN). If the request has been sent already, this method will abort the request.
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()

UPDATE:
As of jQuery 1.5 the returned object is a wrapper for the native XMLHttpRequest object called jqXHR. This object appears to expose all of the native properties and methods so the above example still works. See The jqXHR Object (jQuery API documentation).

UPDATE 2:
As of jQuery 3, the ajax method now returns a promise with extra methods (like abort), so the above code still works, though the object being returned is not an xhr any more. See the 3.0 blog here.

UPDATE 3: xhr.abort() still works on jQuery 3.x. Don't assume the update 2 is correct. More info on jQuery Github repository.

How to abort pending jquery ajax request when user navigates to other page?

According to the ajax documentation, you're doing it properly. The $.ajax() function returns an object that has an abort function. You should check for existence of the function because it might not exist if the request has already finished.

componentWillUnmount: function(){
if (this.ajaxRequest && this.ajaxRequest.abort){
this.ajaxRequest.abort()
}
}

http://api.jquery.com/jquery.ajax/#jqXHR

Stop multiple AJAX requests in same page

You can use websocket .

Or do a checksum file, and tell to ajax to download the checksum file . So you can save data . But, without websocket, you need ajax request every times for update your content

How to cancel/abort jQuery AJAX request?

The jquery ajax method returns a XMLHttpRequest object. You can use this object to cancel the request.

The XMLHttpRequest has a abort method, which cancels the request, but if the request has already been sent to the server then the server will process the request even if we abort the request but the client will not wait for/handle the response.

The xhr object also contains a readyState which contains the state of the request(UNSENT-0, OPENED-1, HEADERS_RECEIVED-2, LOADING-3 and DONE-4). we can use this to check whether the previous request was completed.

$(document).ready(
var xhr;

var fn = function(){
if(xhr && xhr.readyState != 4){
xhr.abort();
}
xhr = $.ajax({
url: 'ajax/progress.ftl',
success: function(data) {
//do something
}
});
};

var interval = setInterval(fn, 500);
);

how to stop ajax request after type:Post

All you want is to abort the $.ajax call, right? take a look to Abort Ajax requests using jQuery for the answer.

However, you cannot abort it from PHP. The $.ajax is running in your client browser, not in your PHP server. And the $.ajax call runs PHP in a separate thread, vs the PHP which loads your view.
All you can eventually do is when you want to load a new view, you certainly have an action on the browser made by the client. Before sending the request to your PHP server, you may want to abort the $.ajax call first.
Another way is to have a PHP shared memory variable, that you set when loading the view, and implement your $.ajax PHP call handler suck a way that you stop processing if that variable is set for that session. A bit complicated vs simply let it go.

How to disable a page on ajax request

Here is the sample code for displaying spinner until ajax response:

HTML Spinner code:

<div id="loading-overlay">
<div class="loading-icon"></div>
</div>

CSS:

#loading-overlay {
position: absolute;
width: 100%;
height:100%;
left: 0;
top: 0;
display: none;
align-items: center;
background-color: #000;
z-index: 999;
opacity: 0.5;
}
.loading-icon{ position:absolute;border-top:2px solid #fff;border-right:2px solid #fff;border-bottom:2px solid #fff;border-left:2px solid #767676;border-radius:25px;width:25px;height:25px;margin:0 auto;position:absolute;left:50%;margin-left:-20px;top:50%;margin-top:-20px;z-index:4;-webkit-animation:spin 1s linear infinite;-moz-animation:spin 1s linear infinite;animation:spin 1s linear infinite;}
@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }

AJAX:

$.ajax({
url: "YOUR PATH"
type: "PATCH",
data: "YOUR DATA HERE",
beforeSend: function(){
$("#loading-overlay").show();
},
success: function (data, textStatus, jqXHR) {
$("#loading-overlay").hide();
},
error: function (jqXHR, textStatus, errorThrown) {
$("#loading-overlay").hide();
alert("something went wrong");
}
});


Related Topics



Leave a reply



Submit