How can I stop an ajax request (don't wait until an answer arrives)?

If I use Ajax to send a request, and this request will take a lot of time ..... if I want to send an anther request, what should I do?

the current behavior of the second request (I made) until the first request receives a response.

NOTE: I want to make this behavior for the whole application (any new request is executed immediately, without waiting for the first to finish) My application using (Ajax + PHP + jQuery + Symfony)

Suppose the first request takes a long time:

$.ajax ({ type: "GET", url: url1, success: function (html) { // do some thing } }); 

At any time, I want this request to execute and complete the first.

 $.ajax ({ type: "POST", url: url, success: function (html) { // do some thing else } }); 

 var xhrReq; xhrReq = $.ajax(...); // then if you want to stop the rqest and exit use : xhrReq.abort(); 
+4
source share
5 answers

This is a kind of manual process, but you can add a global xhr object and test it on every request. If readistate is loading, abort it:

 var xhr; var loadUrl = function(url) { if ( xhr && xhr.readyState > 0 && xhr.readyState < 4 ) { // there is a request in the pipe, abort xhr.abort(); } xhr = $.get(url, function() { console.log('success', this); }); }; loadUrl('/ajax/'); 
+6
source

The XMLHttpRequest object has an abort function. You can use setTimeout to abort a request that takes too long.

EDIT: if you do not want to use a timer and a new event occurs that should interrupt the previous request, then the event handler should do the following

 if(!this.request) return; // request contains the XMLHttpRequest this.request.onreadystatechange = function() {}; if(this.request.readyState != 4) { this.request.abort(); } 

Then after that you can create a new XMLHttpRequest object.

+2
source

I have worked on this in many ways, and I feel like I have found a working solution. I had a caching process due to which the page crashed to completion (on average 5 seconds). Yes, this works better as a CRON job, but I need to create a caching process for the user without knowing the environment that they use for my CMS. What I did: Create a call inside a variable, and then delete it using hard delete. Removing this seems to remove the wait. This hack seemed to pull expectations from a 5-second average to 325 ms of wait.

 var ignore = $.ajax({ url:something/here.php, type: "GET", url: url1, success: function(){} }); delete ignore; 
+1
source

Ajax request variable definition:

 var xhr; 

Making an ajax call:

 xhr = $.ajax(...); 

Cancel ajax call:

 xhr.abort(); 
0
source

The browser allows you to process only a limited number of requests to the same host (2 or 3, as I recall, depending on the browser).

The workaround for the number of requests is to create fake domains - such as img1.domain.com, img2.domain.com, etc., leading to the same host and randomly using them in requests. Then you can just make the queries you need. The number of domains should be selected depending on the number of requests in order to keep within - 2 requests per domain. Otherwise, the third request will wait until one of them is completed.

It allows you to receive answers from all your inquiries. For example, Google uses it to speed up image loading.

EDIT:

Example: you have http://yourhost.com/ and an alias http://alias.yourhost.com that points to the same place. Then:

 $.ajax ({ type: "GET", url: 'http://yourhost.com/somescript.php', success: function (html) { // do some thing } }); 

and then

 $.ajax ({ type: "POST", url: 'http://alias.yourhost.com/somescript2.php', success: function (html) { // do some thing else } }); 
0
source

Source: https://habr.com/ru/post/1392709/


All Articles