AJAX Data Return - Handling Out of Order Responses

Ok Here's the question:  I have a data display page that takes a set of criteria and returns data from a database based on that search criteria.  My problem is that if I make a request that will take 15 seconds to return, then realize it was a mistake and make a request that only takes 1 second to return I hit an overwriting issue.

The 1 second request will complete first and return the data it was supposed to, but then the 15 second request returns and improperly overwrites all of the data from the 1 second request.  What is the best way to handle this to make sure I'm not allowing out of order returns to squash the data I really want?

It's all Javascript based so please keep reponses to Javascript.  Thanks!
Who is Participating?
Jens FiedererTest Developer/ValidatorCommented:
One common method to deal with this is sending serial numbers in requests, which get returned with responses.
Store the maximum serial number handled.
When a response gets back to you, ignore it unless it has a higher serial number than the current maximum.
Jens FiedererTest Developer/ValidatorCommented:

Maybe my most points for least effort!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.