coder
coder

Reputation: 301

How to combine load & resize with $(window).one("scroll", into one function

I have a function that needs to fire on load, resize and the first time scrolling. How can I combine the following two functions in one, to do that?

Alternative ways are welcome too of course.

$(window).on("load resize",function(e){

});

$(window).one("scroll", function() {

});

Upvotes: 0

Views: 1027

Answers (2)

VadimB
VadimB

Reputation: 5711

You can use var fn = function() {} do store you fn reference and use in both cases:

$(window).on("load resize", fn);

$(window).one("scroll", fn);

Upvotes: 1

Scott Marcus
Scott Marcus

Reputation: 65825

Rather than using anonymous functions, just create a function declaration and call it from both events:

$(window).on("load resize", go);

$(window).one("scroll", go);

function go(e){

}

Upvotes: 1

Related Questions