var xhrCount = 0;function sendXHR() {// sequence number for the current invocation of functionvar seqNumber = ++xhrCount;$.post("/echo/json/", { delay: Math.floor(Math.random() * 5) }, function() {// this works because of the way closures workif (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
var isDataReceived= false, waitTime= 1000;$(function() {// Ajax request sent.var xhr= $.ajax({url: 'http://api.joind.in/v2.1/talks/10889',data: {format: 'json'},dataType: 'jsonp',success: function(data) {isDataReceived= true;$('#info').text(data.talks[0].talk_title);},type: 'GET'});// Cancel ajax request if data is not loaded within 1sec.setTimeout(function(){if(!isDataReceived)xhr.abort();},waitTime);});
var activeRequest = false; //global varvar filters = {...};apply_filters(filters);
//function triggering the ajax requestfunction apply_filters(filters){//prepare data and other functionalitiesvar data = {};//limit the ajax callsif (activeRequest === false){activeRequest = true;}else{//abort if another ajax call is pending$request.abort();//just to be sure the ajax didn't complete before and activeRequest it's already falseactiveRequest = true;}
$request = $.ajax({url : window.location.origin + '/your-url.php',data: data,type:'POST',beforeSend: function(){$('#ajax-loader-custom').show();$('#blur-on-loading').addClass('blur');},success:function(data_filters){
data_filters = $.parseJSON(data_filters);
if( data_filters.posts ) {$(document).find('#multiple-products ul.products li:last-child').after(data_filters.posts).fadeIn();}else{return;}$('#ajax-loader-custom').fadeOut();},complete: function() {activeRequest = false;}});}