0

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() {

});
coder
  • 301
  • 4
  • 18
  • 1
    Give the function a name, then pass it to each: `function someName() { … } $(window).on('load resize', someName).one('scroll', someName);` – Ry- Feb 20 '17 at 13:57
  • How about this one [Run Jquery function on window events: load, resize, and scroll?](http://stackoverflow.com/questions/15665231/run-jquery-function-on-window-events-load-resize-and-scroll) – Bakudan Feb 20 '17 at 14:04

2 Answers2

1

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){

}
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

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);
VadimB
  • 5,533
  • 2
  • 34
  • 48