Assume that I have three elements on a page:
- A textbox that accepts a numeric Contact ID.
- A button that, when clicked, will make an Ajax request to the server to fetch the contact information for the Contact ID that was input into the textbox.
- A
divwhich displays the contact information that was fetched from the server.
Now assume two more things:
- Expect that the roundtrip from the server will take 5 seconds.
- If, during the 5 second delay, the user enters a different number into the textbox, and clicks the button, the web page should ignore the first ajax result and honor the second one (i.e. the most recent Ajax request).
My Question:
- How could I implement a “Last One Wins” jQuery-Ajax Queue like I have described?
- Would I use deferred objects or promise objects to help me solve this problem?
You just handle it all in the success callback. The first thing you check in the callback is if new information is entered, and if so let the new information win. Otherwise go through with the results of the request.
Should be pretty easy to set up flags if necessary. Fly the flag whenever the new request is sent. Use it in the success callback.