Skip to content

Instantly share code, notes, and snippets.

@dankempster
Created June 28, 2011 09:29
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 dankempster/1050804 to your computer and use it in GitHub Desktop.
Save dankempster/1050804 to your computer and use it in GitHub Desktop.
Iterates over an array of objects passing each one to callback after interval.
(function($){
/**
* Iterates over an array of objects passing each one to callback after interval.
* Calls doneCallback when complete.
*/
var version = '1.1';
var timeout = null;
$.slowEach = function(objs, interval, callback, doneCallback) {
if(objs.length>0) {
var x = 0;
iterate();
}
function iterate() {
if( callback.call(objs[x], x) != false ) {
x++;
if( objs.length > x ) {
timeout = setTimeout( iterate, interval );
}
else if($.isFunction(doneCallback)) {
doneCallback.call(objs);
}
}
}
}
$.fn.slowEach = function(interval, callback, doneCallback) {
$.slowEach(this, interval, callback, doneCallback);
}
/**
* Clear all events
*/
$.slowEach.clearTimeout = function()
{
clearTimeout(timeout);
}
/**
* Returns the plug-in's version
*/
$.slowEach.version = function()
{
return version;
}
/**Change log**
* 1.0: First Version
* 1.1: Added clearTimeout function - allows the plugin to be interrupted (thanks @jamesahallam)
*/
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment