Javascript - trying to wrap your head around custom events

I am trying to set up custom events. I understand how to register and fire custom events. However, it seems that it is not possible to register truly custom events. Everything needs to be traced back to the DOM event, such as click, load, blur, etc. Or I'm wrong?

For example, suppose I have an array. I want to register an event that fires when the length of the array changes. As far as I understand, I would have to register an event, and then create a setInterval timer that checks the current length of the array for the previously saved length. If the length has changed, then I would need to fire my custom event from inside setInterval.

Is there a way to register an event for my array and will it fire automatically when the length changes?

+3
source share
2 answers

Custom events are in the W3C spec , but none of the major browsers support them. This is why several other users have suggested third-party libraries. Prototype, YUI, jQuery and most others have these features. You can also flip your own .

If you want to see how some kind of custom event syntax might look like, you can take a look at this tutorial for custom Prototype events . Some important points:

  • DOM, .
  • prefix:event,
  • memo, .
+3

addElement , , , , array.length. removeElement. .

+2

Source: https://habr.com/ru/post/1703549/


All Articles