Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created March 30, 2011 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/894952 to your computer and use it in GitHub Desktop.
Save cowboy/894952 to your computer and use it in GitHub Desktop.
Like jQuery's .one but runs only once, across multiple elements and/or event types.
/*!
* jQuery oneOnly - v0.1pre - 03/30/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.oneOnly = function( types ) {
var elems = this,
args = Array.prototype.slice.call(arguments),
fn = args.pop();
function handler() {
elems.unbind(types, handler);
return fn.apply(this, arguments);
}
return this.bind.apply(this, args.concat(handler));
};
})(jQuery);
@cowboy
Copy link
Author

cowboy commented Mar 30, 2011

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