Any way to bind event handler and call it at the same time?

I have some code like the following:

var f = function() {...};

$(window).resize(f);

f();

As you can see, I'd like f to be called every time the window is resized, and also for f to be called once after I make it the resize event handler.

Is there any way I can make this code shorter/more succinct? Ideally I'd like to eliminate the need for an f variable and just make it an anonymous function (i.e. $(window).resize(function() {...}), but I need some way of calling the function once...

Upvotes: 3

Views: 69

Answers (2)

user2153497
user2153497

Reputation: 356

$(window).
resize(function () {console.log("stop resizing me!");}).
resize();

http://jsfiddle.net/KmhZf/1/

Upvotes: 2

epascarello
epascarello

Reputation: 207527

Well you will not make it shorter, but this is one way you can trigger it instead of the f().

$(window).resize(f).trigger("resize");

Upvotes: 6

Related Questions