This version of cachedGet only allows out one request for each
URL. If it's called again with a URL that it's already seen,
it adds that to the callback handler for the first request for
that URL.
var gPendingRequests = {}; var gRequestCache = {}; $.cachedGet = function(url, k) { if (url in gRequestCache) k(gRequestCache[url], 'success'); else if (url in gPendingRequests) gPendingRequests[url].push(k); else { var queue = [k]; gPendingRequests[url] = queue; $.get(url, function(result, status) { if (status == 'success') gRequestCache[url] = result; while (queue.length) queue.shift().call(this, result, status); delete gPendingRequests[url]; }); } };
$.cachedGet('services/random', log); $.cachedGet('services/random', log); $.cachedGet('services/echo/1', log); $.cachedGet('services/echo/2', log); $.cachedGet('services/random', log);