Reputation: 301
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
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
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