Here's a different kind of throttle. This one only allows
a certain number (two) of outstanding requests; it queues
subsequent requests until one of outstanding requests
has returned.
A production version would need to check
for error conditions too.
var gQueue = []; var gOutstanding = 0; $.throttled = function(url, k) { function k2() { gOutstanding--; k.apply(this, arguments); if (gOutstanding < 2 && gQueue.length) { var entry = gQueue.shift(); $.get(entry[0], entry[1]); } } if (gOutstanding < 2) { gOutstanding++; $.get(url, k2); } else gQueue.push([url, k2]); };
for (var i = 0; i < 10; i++) $.throttled('services/sleep/2', log);