jquery.unevent.js
jquery.unevent.js copied to clipboard
Managing multiple callbacks for the same pair event + element
Hello yckart,
it would be great to have the possibility of registering multiple callbacks for the same pair event + element.
Something like this for instance :
$(window).on('scroll', function(){
// Do something.
}, 200);
$(window).on('scroll', function(){
// Do something.
}, 400);
For now it is not possible because the latter will always override the former. (because for jquery they will get the same callback, defined at line 20)
Regards,
Hi, just a little trick ~~(untested)~~ edit : seems work nicely
;(function ($) {
$.fn.afterEvent = function () {
var args = Array.apply(null, arguments);
var last = args[args.length - 1];
if (isNaN(last) || (last === 1 && args.pop())) return $.fn.on.apply(this, args);
var delay = args.pop();
var fn = args.pop();
args.push(function () {
var self = this;
var params = arguments;
if (!this.timer) this.timer = {};
if (this.timer[delay]) clearTimeout(this.timer[delay]);
this.timer[delay] = setTimeout(function () {
fn.apply(self, params);
}, delay);
});
return $.fn.on.apply(this, args);
};
}(this.jQuery));
Hi jpouillard, thank for your reply. But in fact I have worked on an alternative because this plugin has some other drawbacks : https://github.com/tonai/jquery-onend