Created
March 17, 2010 21:30
-
-
Save floere/335731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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