Last active
December 12, 2015 04:38
-
-
Save yulanggong/4715924 to your computer and use it in GitHub Desktop.
运行事件回调的同时禁用当前元素,避免相同的回调再次被触发,在回调中可以调用 enable 函数解除禁用。
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($, 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