Last Updated: September 29, 2021
·
599
· ivanca

jQuery Event to execute a callback *right now*

Sometimes whe want to execute something now AND when an event happens; the most common way this happens is in resize events, so instead of doing this:

fixSize();
$(window).resize(fixSize);

We can do this:

$(window).on('now resize', fixSize);

To be able to do this just add this lines after loading jQuery:

jQuery.event.special.now = {
     setup: function(a, b, cb) {
          var self = this;
          setTimeout(function (argument) {
            $(self).trigger('now').off('now');
          });
     }
};