0

I would like to immediately execute an event after I bind it. For example:

$(window).resize(function () {
    alert("Some code");
});

Typically what I've done in the past is:

$(function() {
    $(window).resize(resizeFunc);
    resizeFunc(); //This will execute immediately
});

function resizeFunc() {
    alert("Some code");
}

But that is not helpful for anonymous functions, and it is a bit verbose. I'd like to do something like: $(window).resize(function () {})(); or $(window).resize(function () {}).trigger(); but those don't work.

Is there a way to immediately execute anonymous functions on bind?

Eric
  • 654
  • 5
  • 16
  • 3
    You need to trigger the specific event `.trigger('resize')`. – Heretic Monkey Jun 07 '19 at 12:08
  • You can use a callback, but I don't really understand what result you want to achieve – GalAbra Jun 07 '19 at 12:12
  • Thank you, Heretic. That was the exact answer! Regarding the duplicate, I did many searches and couldn't find where this was answered (though I was surprised). – Eric Jun 07 '19 at 12:24

0 Answers0