jQuery.fn.on
指定された数のイベントが発生した後に、デフォルトのハンドラーを呼び出す関数を作成しました。オリジナルevent
が関数に渡されないため、これを改善する方法はありますか?
;(function ($) {
var oldOn = $.fn.on,
i = 0;
$.fn.on = function () {
var args = arguments,
j = args.length;
for (var last in args);
while (j--) {
if ($.isFunction(args[j]) && !isNaN(args[last])) {
var oldFn = args[j],
after = args[last];
args[j] = function () {
i++;
if (i === after) {
oldFn.call();
i = 0;
}
};
}
}
if (!isNaN(args[last])) delete args[last];
return oldOn.apply(this, arguments);
};
})(jQuery);
// call the plugin and fire the `fn` after each 20 mousemoves
$(document).on('mousemove', function (e) {
console.log(e); // undefined
}, 20);
ご覧のとおり、次は問題なく動作します。
var oldOn = $.fn.on;
$.fn.on = function () {
return oldOn.apply(this, arguments);
};
$(document).on('click', function(e){
console.log(e) // jQuery.Event
});
間違いはどこですか、どうすればこれを機能させることができますか?
アップデート
今はもっと簡単になりました: https://github.com/yckart/jquery.unevent.js