0

I send several ajax request at a time. But as you know that the result arrival time isn't related with the time you send request. Now, this make a problem to me.

Here's what I face now.

When I click the TAB button made with HTMLElement, it sends ajax call.

If I click the tab just after clicking other tab. two request goes together and I can't know which one could response to me first. but unfortunately last one sometime arrive earlier than the first ajax request.

then, it occur my html markup messy, so my question is is there any way to make ajax call arrive in regular sequence.

initialize : function() { //<!-- this function call AJAX request
 this.getActivatedDeviceList();
 this.getDeActivatedDeviceList();
 this.getLostOrStolenDeviceList();
 this.getWaitingDeviceList();
},

getActivatedDeviceList : function() {
 var url = "/monitor/device/getActivatedDeviceJson/" + TopMenu.CURRENT_TAB + "/";
 this.loadTemplateAndFillUp(url, "#activatedDeviceTemplate", "#activatedDevice");
},
getDeActivatedDeviceList : function() {
 var url = "/monitor/device/getDeactivatedDeviceJson/" + TopMenu.CURRENT_TAB + "/";
 this.loadTemplateAndFillUp(url, "#deactivatedDeviceTemplate", "#deactivatedDevice");
},
getLostOrStolenDeviceList : function() {
 var url = "/monitor/device/getLostOrStolenDeviceJson/" + TopMenu.CURRENT_TAB + "/";
 this.loadTemplateAndFillUp(url, "#lostOrStolenDeviceTemplate", "#lostOrStolenDevice");
},
getWaitingDeviceList : function() {
 var url = "/monitor/device/getWaitingDeviceJson/" + TopMenu.CURRENT_TAB + "/";
 this.loadTemplateAndFillUp(url, "#waitingDeviceTemplate", "#waitingDevice");
},
loadTemplateAndFillUp : function(url, templateElement, appendElement) {
 $.ajax({ //<!--This ajax call fires upon initialize function
 url : url,
 dataType : 'json',
 beforeSend : function(xhr) {
 $(appendElement).children(".zoom").attr("src", "/monitor/img/icon/loading.gif");
 },
 complete : function(xhr) {
 setTimeout(
 function() {
 $(appendElement).children(".zoom").attr("src", "/monitor/img/btn/btn_zoom.png")
 }, 
 500
 );
 $(appendElement).find("table.dataBoxTable").tableScroll({height:DeviceManager.TABLE_HEIGHT});
 DeviceManager.columnAutoFit(appendElement);
 DeviceManager.addListenerAndHandler();
 },
 success : function(data) {
 $(templateElement).tmpl(data).appendTo(appendElement); //<!--This jquery template get messed up by ajax arrival order.
 },
 async: true
 });
},

I hope you could understand my English. thank you very much.

asked May 17, 2012 at 11:37
2
  • I don't want to use async: false because I can't see loading image.. Commented May 17, 2012 at 11:41
  • 1
    You may try this stackoverflow.com/questions/446594/… Killing the last request and call ajax again Commented May 17, 2012 at 11:48

1 Answer 1

1

Client side: Have a counter in the client side i.e say ajaxCounter = 0; and on ajax call increment the counter and then send the counter as parameter to the call.

Now as there are two calls made then ajaxCounter is 2

Server side: Always return the ajaxCounter received as the parameter. So for the 1st ajax call the returned will be 1. So for the 2nd ajax call the returned will be 2.

Client side: In the client side check whether the ajaxCounter == counterReturnedFromClient. If it is equal execute the complete method otherwise ignore.

answered May 17, 2012 at 13:25
Sign up to request clarification or add additional context in comments.

1 Comment

thanks you ! instead using counter i used ID generate to identifying last ajax call.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.