Skip to content

Instantly share code, notes, and snippets.

@floere
Created March 17, 2010 21:30
Show Gist options
  • Save floere/335731 to your computer and use it in GitHub Desktop.
Save floere/335731 to your computer and use it in GitHub Desktop.
// Say, you want only to use the latest arriving ajax request.
// For example in a search engine, where only the request of the
// most recently sent request is of interest. Responses that come
// after and have been sent before the one that has already arrived
// are ignored:
// remember the latest request date
//
var latestRequestDate = new Date();
function responseHandler() {
// bind a timestamp to the current time
var requestDate = new Date();
// This function only does what it does if the
// request is the most recently sent request and none other
// has arrived earlier.
return function(response) {
if (requestDate < latestRequestDate) { return; }
latestRequestDate = requestDate;
// handle the response here
// …
//
};
}
// Where you send the request
//
$.get(url, params, responseHandler(), 'json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment