Handler or Listeners. What is better use for notification of event? What is faster, more efficient etc.?
Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.
Summary: addEventListener can add multiple events, whereas with onclick this cannot be done. onclick can be added as an HTML attribute, whereas an addEventListener can only be added within <script> elements. addEventListener can take a third argument which can stop the event propagation.
It could happen to copy-paste page, just as ordinary node. In this case EventListener will fire, but EventHandler will not. That is the key difference between them.
TLDR; Always remove event listeners when you don't plan on using them any longer.
That's a good question!
scenario for using a handler
I've got an Android background service running in my app that uses handlers exclusively for web communications - I decided to go this route because the handler will queue requests and execute them one by one so then I know that a sequence is remained intact.
For example, in an instant messenger app you might find it desirable to maintain a sequence for your chatting.
scenario for using a callback
My background service also uses a class that reads from hardware (in a separate thread); some data might come in at any time and needs to be processed immediately. For that class I implemented a listener/callback interface.
My only question is whether there's any etiquette for the size of the handler. I have about 50 unique messages:
The handler requires about 60% of the service's code - as you can imagine this results in a very big switch(case{}) structure (almost 1000 lines of code). Too big? How to break it apart?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With