3

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...

4

2 回答 2

6

好吧,你不会让它更短,但这是你可以触发它而不是f().

$(window).resize(f).trigger("resize");
于 2013-04-18T13:31:06.970 回答
2
$(window).
resize(function () {console.log("stop resizing me!");}).
resize();

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

于 2013-04-18T13:33:33.960 回答