Same jQuery event, separate function

Let's say that I have a function that should execute on $(document).mousemove() . Then I decided that I had another function that should be executed in the same event. Perhaps these two functions are not related to each other, so in the name of modularity, you could use two separate functions to listen to $(document).mousemove() .

Is this a bad shape? Are there any implications for work?

+7
source share
3 answers

jQuery will automatically add events to the chain, so there is no problem with having duplicate handler descriptions for the same event. If you can combine the two definitions of a handler into one reasonably, then of course there is nothing wrong with this approach, but there is little obvious value, except perhaps for readability.

+2
source

Can you not just do it? If I do not understand your question

 $(document).mousemove(function(){ callOneFunction(); callTwoFunction(); }); 
0
source

Of course, if you decide to install 1000 functions that listen to one event, I would say something is wrong with your design, but for me, two functions are just ok.

I would go with name handlers to maintain maintainability, at least if St. John developed it in jQuery, it is worth using in some cases, so you do this:

 $(elem).bind('mousemove.ns1', funcOne); $(elem).bind('mousemove.ns2', funcTwo); 

that way you can also unleash one by one

 $(elem).unbind('mousemove.ns1'); $(elem).unbind('mousemove.ns2'); 

Or even

 $(elem).unbind('.ns1'); 
0
source

All Articles