Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created March 12, 2010 20:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cowboy/330793 to your computer and use it in GitHub Desktop.
Save cowboy/330793 to your computer and use it in GitHub Desktop.
jQuery queueFn: add a $.fn method or function onto the effects queue
/*!
* jQuery queueFn - v0.5 - 06/18/2010
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($){
'$:nomunge'; // Used by YUI compressor.
var defaults = {
queue: 'fx'
};
$.fn.queueFn = function() {
var args = Array.prototype.slice.call( arguments ),
options = $.extend( {}, defaults, typeof args[0] === 'object' && args.shift() ),
fn = args.shift();
fn = $.isFunction( fn ) ? fn : $.fn[ fn ];
return this.queue( options.queue, function(next){
fn.apply( $(this), args );
next();
});
};
})(jQuery);
// usage:
//
// $('#foo').fadeOut().queueFn( 'remove' );
//
// $('#bar').hide().addClass( 'fading' ).fadeIn().queueFn( 'removeClass', 'fading' );
//
// function remove( state ){ state && $(this).remove(); };
// $('#baz').fadeOut().queueFn( remove, some_condition );
@tylik
Copy link

tylik commented Jun 18, 2010

Thank you so much! This is exactly what I wanted. I believe that this code should be included in jquery's core as it is rather useful.

@cowboy
Copy link
Author

cowboy commented Jun 20, 2010

This gist has now been "pluginized" and is available here: jQuery queueFn.

Note that v0.5 shown here is overkill. Since nobody using this plugin is going to want to use a custom animation queue, v0.4 is what's currently "live" as the official plugin.

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