1

I am using jQuery.ajax for my site where user can select multiple check box. And from the selected check box user will get the data.

For every select box it is doing ajax request and showing result.

But i am facing one problem where suppose user select 1st check box then again it is deselecting the 1st check box and again selecting 2nd check box. So for selecting first check box it is doing 1 st ajax call and when user deselect 1st select box it is doing 2nd ajax call then same for 2nd select box it is doing 3rd ajax call.

so simultaneously 3 ajax calls are active but i want a way to abort all previous ajax call those are still in pending or in progress and show result as per the last/recent ajax call.

Code i am using for Ajax call:

   jQuery.ajax({url: '/xyz.php',
    success: function(data) {
    //do something
    }
});
Uooo
  • 6,204
  • 8
  • 36
  • 63
Saurabh
  • 33
  • 4
  • Possibly easiest to wait until they're all done, then act on the most recent one only? http://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-request-are-done – StackExchange What The Heck Sep 12 '13 at 11:06
  • what if every time you clciked a random number variable was generated and if you post the varibale with in object sent then you return it and only if the posted variable matches the one created then continue. or only allow a click on the other the other boxes once the ajax is finished. – Hello-World Sep 12 '13 at 11:08
  • aborting request client side is one thing, make it aborted server side is an other – A. Wolff Sep 12 '13 at 11:12
  • @A. Wolf -> that sounds very dangerous right? – Hello-World Sep 12 '13 at 11:55

2 Answers2

2

You can do like this.

var CurrentAjaxReq = null;

if(CurrentAjaxReq != null)
{
CurrentAjaxReq.abort(); 
}

CurrentAjaxReq = jQuery.ajax({url: '/xyz.php',
success: function(data) {
 CurrentAjaxReq  = null;
//do something
}
});
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
1

hy you can abort the ajax call first,

var myAjax = jQuery.ajax({url: '/xyz.php',
success: function(data) {
//do something
}

});

//abort first and than send new ajax
myAjax .abort()
Mansoor Jafar
  • 1,458
  • 3
  • 15
  • 31
  • Thanks, but i have already tried this and this seems to be abort all ajax call. Here i need to abort all ajax call except to recent one. – Saurabh Sep 12 '13 at 11:22