Out of order Ajax responses

I have several links on my Website that make Ajax calls and replace an div with the response. If I click several of these links quickly, it looks like the responses may replace the wrong div. Response times of the server can vary wildly, so responses may arrive out of order. Is there a solution other than always responding with Javascript that replaces the correct div?

TIA,   Jeffrey

I have several links on my Website that make Ajax calls and replace an div with the response. If I click several of these links quickly, it looks like the responses may replace the wrong div. Response times of the server can vary wildly, so responses may arrive out of order. Is there a solution other than always responding with Javascript that replaces the correct div?

How are you choosing which div gets replaced at the moment?

Fred

Don't forget the first "a" of AJAX is for "asynchronous". How does each call identify which div it's supposed to update? Could you be gettings DOM IDs duplicated/confused in your calls?

Quoting Frederick Cheung <frederick.cheung@gmail.com>:

> I have several links on my Website that make Ajax calls and replace an div > with the response. If I click several of these links quickly, it looks like > the responses may replace the wrong div. Response times of the server can > vary wildly, so responses may arrive out of order. Is there a solution other > than always responding with Javascript that replaces the correct div? > How are you choosing which div gets replaced at the moment?

A link_to_function calls the jQuery code shown below. 'id' is the primary key of the model for the div surrounding the link. So the div surrounding the link is replaced with the response from the server.

function replace_article(id, action) {   obj = $j('#article_'+id);   jQuery.ajax({url: '/articles/'+action+'/'+id, type: 'POST',       data: {_method: 'put'},       success: function(data){obj.replaceWith(data);}         }); }

Jeffrey

Look closely at your function, what is the scope of the variable named obj? Ask yourself what happens when multiple calls to replace article are sharing a global reference to obj because you forgot to say var obj.

Quoting Ramon Leon <ramon.leon@allresnet.com>: