Created
June 28, 2011 09:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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