Skip to content

Instantly share code, notes, and snippets.

@sj26
Created December 5, 2011 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sj26/1432538 to your computer and use it in GitHub Desktop.
Save sj26/1432538 to your computer and use it in GitHub Desktop.
/*
TrafficCop
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.1.0
*/
(function($, undefined) {
var inProgress = {};
$.trafficCop = function(url, options) {
var reqOptions = url, key;
if(arguments.length === 2) {
reqOptions = $.extend(true, options, { url: url });
}
key = JSON.stringify(reqOptions);
if (key in inProgress) {
for (i in {success: 1, error: 1, complete: 1}) {
inProgress[key][i](reqOptions[i]);
}
} else {
inProgress[key] = $.ajax(reqOptions).always(function () { delete inProgress[key]; })
}
return inProgress[key];
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment