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

On May 5, 4:20pm, “Jeffrey L. Taylor” [email protected] wrote:

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

On 5 May 2011 16:20, Jeffrey L. Taylor [email protected] wrote:

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?

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 C. [email protected]:

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

Quoting Ramon L. [email protected]:

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.

Duh. Just read about Coffeescript and how it saves you from this
mistake.
Didn’t connect it with my own code.

Thank you,
Jeffrey

On 05/05/2011 08:41 AM, Jeffrey L. Taylor wrote:

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.


Ramon L.