Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Created November 30, 2010 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addyosmani/721238 to your computer and use it in GitHub Desktop.
Save addyosmani/721238 to your computer and use it in GitHub Desktop.
(function($) {
$.fn.recurse = function(closure, options) {
var self = this, settings = $.extend({}, $.fn.recurse.defaults, options);
self.queue(settings.queue, function(next){
if (!closure.apply(self, [self])) {
self.delay(settings.delay, settings.queue).queue(settings.queue, arguments.callee).trigger('queue').dequeue(settings.queue);
} else {
self.trigger('complete');
}
});
self.dequeue(settings.queue);
return this;
}
$.fn.recurse.defaults = {
delay: 100,
queue: (+new Date).toString(36)
};
})(jQuery);
// Forked from https://github.com/dsimard/jskata/blob/master/src/jskata.nofreeze.js
function loop(condition, increment, func, options, callback) {
var timer, sleep = options && options.sleep || 1, chunk = options && options.chunk || 10;
function stop() {
clearTimeout(timer);
if (callback) {
callback(condition());
}
}
(function(){
var c = 0;
while (condition() && (c++ < chunk)) {
func();
if (increment) {
increment();
}
}
if (condition()) {
timer = setTimeout(arguments.callee, sleep);
} else {
stop();
}
})();
return {stop:stop};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment