Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created March 2, 2011 02:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cowboy/850322 to your computer and use it in GitHub Desktop.
Save cowboy/850322 to your computer and use it in GitHub Desktop.
jQuery seq: execute code sequentially, for each selected element.
/*!
* jQuery seq - v0.1 - 03/1/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($){
$.fn.seq = function( fn, done ) {
var elems = this;
if ( elems.length ) {
fn.call( elems[0], function() {
$.fn.seq.call( elems.slice(1), fn, done );
});
} else if ( done ) {
done();
}
return elems;
};
})(jQuery);
elems.hide().seq(function(next){
$(this).fadeIn( 100, next );
});
elems.hide().seq(function(next){
$(this).fadeIn( 100, next );
}, function(){
console.log( "done!" );
});
(function loopy(elems){
elems.hide().seq(function(next){
$(this).fadeIn( 100, next );
}, function(){
loopy( elems.slice(1) );
});
})( $("li") );
@cowboy
Copy link
Author

cowboy commented Mar 10, 2011

@cowboy
Copy link
Author

cowboy commented Mar 14, 2011

Possible todos:

  • pass i, elem into callback (like each)
  • return a deferred (is this even sane?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment