cachedGet only submits the request if it hasn't seen a response
to that same request. Otherwise it re-uses the previous response.
Note that the first two requests for 'services/random' still get
different answers, though, because the second goes back before
the first response returns. The final request might get one of
these same two answers, if the five-second delay is long enough
for one of the first two requests to return.
var gRequestCache = {}; $.cachedGet = function(url, k) { if (url in gRequestCache) k(gRequestCache[url], 'success'); else $.get(url, adviseBefore(k, function(result, status) { if (status == 'success') gRequestCache[url] = result; })); };
$.cachedGet('services/random', log); $.cachedGet('services/random', log); $.cachedGet('services/echo/1', log); $.cachedGet('services/echo/2', log); setTimeout(function() {$.cachedGet('services/random', log);}, 5000);