The EventTarget interface is used by objects that can receive events and have listeners set up to handle those events. This interface provides three methods that are implemented by any event target. The most commonly used event targets are element and their children, as well as Document and Window. However, other objects such as XMLHttpRequest, AudioNode, and AudioContext can also be event targets.

In addition to implementing the EventTarget interface, many event targets like elements, documents, and windows also allow setting event handlers through “onevent” properties and attributes.

Constructor

EventTarget() Creates a new EventTarget object instance.

Instance methods

EventTarget.addEventListener() Registers an event handler of a specific event type on the EventTarget.

EventTarget.removeEventListener() Removes an event listener from the EventTarget.

EventTarget.dispatchEvent() Dispatches an event to this EventTarget.

In an Azion function, EventTarget is the foundation behind the global addEventListener used to register the fetch handler, and it can also back your own objects that need to emit and listen for custom events during request processing.

Use cases

  • Building custom objects that dispatch and listen for events while a request is being handled on Azion’s global network.
  • Composing event-driven logic on top of the same interface that powers the runtime’s fetch event.
  • Adding and later removing listeners dynamically to control which handlers react to a given event.

For more information on EventTarget visit MDN Web Docs.