Skip to content

Instantly share code, notes, and snippets.

@yulanggong
Last active December 12, 2015 04:38
Show Gist options
  • Save yulanggong/4715924 to your computer and use it in GitHub Desktop.
Save yulanggong/4715924 to your computer and use it in GitHub Desktop.
运行事件回调的同时禁用当前元素,避免相同的回调再次被触发,在回调中可以调用 enable 函数解除禁用。
;(function($, undefined){
/**
* @param {String} types event types
* @param {String} [selector]
* @param {Function} fn function(e, enable){}
* @param {Object} [option]
* @return {Object} jQuery Object
* @version 0.3
*/
$.fn.ice = function(types, selector, fn, option){
if (typeof selector == 'function' ){
option = fn;
fn = selector;
selector = undefined;
};
option = $.extend({
onDisable: function(){},
onEnable: function(){}
}, option);
this.on(types, selector, function(e) {
var that = this,
$that = $(that);
if ($that.is('.disabled')) return false;
$(this).addClass('disabled');
option.onDisable.call(that, e);
return fn.call(that, e, function(){
$that.removeClass('disabled');
option.onEnable.call(that, e);
});
});
return this;
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment