Skip to content

Instantly share code, notes, and snippets.

@Overbryd
Created November 5, 2010 20:50
Show Gist options
  • Save Overbryd/664845 to your computer and use it in GitHub Desktop.
Save Overbryd/664845 to your computer and use it in GitHub Desktop.
abort()ing previous xhr requests to avoid callback race conditions
var API = {
getJSON: function(path, params, success){
var that = this;
if (_.isFunction(params)){
success = params;
params = null;
}
if(this.runningRequest)
this.runningRequest.abort();
this.runningRequest = $.ajax({
url: path,
data: params,
complete: function(xhr, textStatus){
that.runningRequest = null;
if(xhr.status==200)
success.call(that, $.parseJSON(xhr.responseText), textStatus);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment