Custom Events
Custom Events
Custom events allow developers to create their own events with names and options for the event to execute when fired. Custom events are useful when a developer wants certain things to happen around set parameters but can't find an existing event that works with said parameters.
Creating Custom Events
Creating a custom event works by using the even constructor CustomEvent()
. This constructor has two parameters; eventType
, which is the name of the event, and options
, which is what is set to happen when the event fires.
Why Custom Events?
Like noted above, custom events exist simply because not everything a user can do on the internet has a pre-built event for it. An example would be if a developers wants something to happen when the user sends a message on the new live chat feature his site is introducing. Well there isn't a prebuilt event for that, so a custom event would need to be created.
Summary
To put it simply, custom events exist as a way to give developers more freedom to make their own specific events for specific needs. While it wouldn't necessarily be back breaking without them, it allows for a much higher level of interactivity for the users, which allows for a great UX.