jquery.unevent.js icon indicating copy to clipboard operation
jquery.unevent.js copied to clipboard

Managing multiple callbacks for the same pair event + element

Open tonai opened this issue 10 years ago • 2 comments

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,

tonai avatar May 04 '15 13:05 tonai

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));

jpouillard avatar Jul 17 '15 13:07 jpouillard

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

tonai avatar Jul 19 '15 16:07 tonai