web api instance method
EventTarget: dispatchEvent() method
Available in workers
The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected
event listeners in the appropriate order. The normal event processing
rules (including the capturing and optional bubbling phase) also apply to events
dispatched manually with dispatchEvent().
Calling dispatchEvent() is the last step to firing an event. The event
should have already been created and initialized using an Event() constructor.
[!NOTE] When calling this method, the
targetproperty is initialized to the currentEventTarget.
Unlike “native” events, which the browser fires by queuing a task on the event loop, dispatchEvent() invokes all applicable event handlers synchronously before returning. The read-only isTrusted property is true for native events and false for events dispatched with dispatchEvent().
Syntax
dispatchEvent(event)
Parameters
event- : The
Eventobject to dispatch. Itstargetproperty will be set to the currentEventTarget.
- : The
Return value
false if event is cancelable, and at least one of the event handlers which received event called preventDefault(). Otherwise true.
Exceptions
InvalidStateErrorDomException- : Thrown if the event’s type was not specified during event initialization.
[!WARNING] Exceptions thrown by event handlers are reported as uncaught exceptions. The event handlers run on a nested callstack; they block the caller until they complete, but exceptions do not propagate to the caller.
Example
See Creating and dispatching events.