Abort ajax request

Asked

Viewed 2,030 times

4

I have a page where an ajax event grid is loaded. I also have on this screen a menu of actions.

Once accessed, the page is loaded (including the action menu), and the events grid, being heavier is loading separately.

What’s happening is that while the events table doesn’t fully load, I can’t trigger an action.

Example: I enter the page and the events grid starts loading (I see in the console the pending request) When I click on any link, the request is made, but only completed after the event grid finishes loading. At this moment, two pending requests are in the console only: the one of the event grid, and the one of the link I clicked.

Somebody’s got a light on how to fix this problem?

All requisitions are asynchronous. I think about aborting pending requests when clicking on an action, but how??

  • Would this answer your question? http://answall.com/questions/22589/interrup%C3%A7%C3%A3o-de-uma-requisi%C3%A7%C3%A3o-ass%C3%Adncrona

2 answers

5

When you use the method $.ajax() jQuery returns an object that you can store in a variable. So:

var ajax = $.ajax(...);

this object has its own methods and one of them is the .abort(), so just use ajax.abort(); to cancel the order.

You can also solve the problem with the design of the code itself. That is, planning in the code to address this problem. But to help, I need to see how you got the code now. However a solution is the one I suggested above and what you yourself are looking for: cancel the order ajax.

  • I did some research on . abort(), but when would I call him?? 3

  • @Gabriel can call when he is waiting for the answer. In the case of the/flow code you described you can call when you click on a new link.

  • I did what you proposed, Sergio. What is happening is that the request is aborted by clicking on a link, but the time it is taking to load the page of the link is the same as it took while waiting for the end of the request of the events grid. That is, it is as if the request only changes the status to 'canceled', but the next request still waits for the one that is canceled 'to end'.

  • @Gabriel can you put the code you have in the question? so you can see if the problem is there or on the server side.

  • Unfortunately not Sergio. A test I did: in the php function called by ajax, I put a Sleep(20) and cleaned my ajax sequence. What happens is that even if you cancel the request, the clicked link is only opened (even in another tab), after this stipulated time..

  • @Gabriel without seeing his jQuery it’s hard to help more... can’t put part for me to understand how this code is going?

Show 1 more comment

0

You can do recursively, it only loads the next one at the end of the current one, so it is possible to enter a request in the middle and it abort the rest, loading what you want without waiting for the whole grid to be loaded.

As I have the same problem the solution was as follows

var arrTabela;//contem um array de id's para a consulta de cada linha da grade
var tot = 4; //pegar o total de linhas da tabelas 

function teste(arrTabela) {
    if(arrTabela < 0) return;
    return $.ajax({
        url : 'minha url q retorna o total',
        data: {id : arrTabela[0]},
        dataType: 'json',
        success: function(data){
            //subistitui o valor desejado
            $("#tot-list"+id).html(data);
            //remove o item usado no array
            arrTabela.splice(1,1);
            tot--;
            //chama novamente a função
            teste();
        }
    });
};

It wasn’t exactly like this, but the idea is this, sorry for the syntax errors that must have many, but I made this one in my head. I hope it helps!

  • had forgotten the main thing is to call the function again at the end of it but already this ok :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.