Node.js eventTarget.dispatchEvent() method


The eventTarget.removeEventListener() method is used to remove the event listener which is previously added with eventTarget.addEventListener() method from the list of handlers for the event type.

This method belongs to EventTarget class of node:events module.

Syntax

Following is the syntax of the NodeJs eventTarget.removeEventListener() method −

eventTarget.removeEventListener(type, listener, [, options])

Parameters

This method accepts three parameters as described below.

This method has three parameters described below.

  • type: This parameter holds a string value that represents the event type to listen for.
  • listener: The object that receives a notification when an event of the specified type occurs.
  • options: (Optional)
    • once: A Boolean value that indicates that the listener should be called at least at most once after being added. If it is true, then the listener would be automatically removed when it is invoked.
    • passive: A Boolean value is true it serves as a hint that the listener wont call the Events objects preventDefault() method.
    • capture: A Boolean value that indicated that the events of this type will be dispatched to the specified listener before being dispatched to any EventTarget. This is not directly used in Node.js, it is just added for API completeness.
    • signal: Whenever the specified AbortSignal objects abort() method is called the listener will be removed.

Example 1

Following is the basic example of the NodeJs eventTarget.removeEventListener() Method.

First, we imported the node:events module passing a listener function with an event type foo to the eventTarget.addEventListener() method. Then we removed the instance of the handler from the event type foo by calling the eventtarget.removeEventListener().

const { EventEmitter, listenerCount } = require('node:events');

function handler(event){
};

const eventtarget = new EventTarget();

eventtarget.addEventListener('foo', handler);

eventtarget.removeEventListener('foo', handler);

Output

As per the above program, we added one listener handler to the event type foo and then we removed the handler from the foo.

Example 2

In this program, we passed a single listener function handler with an event type foo to the eventTarget.addEventListener() method. Then we called the eventtarget.removeEventListener() by passing handler as a parameter.

const { EventEmitter, listenerCount } = require('node:events');

function handler(event){
};

const eventtarget = new EventTarget();

eventtarget.addEventListener('foo', handler);
eventtarget.addEventListener('foo', handler);
eventtarget.addEventListener('foo', handler);

eventtarget.removeEventListener('foo', handler);

Output

As per the above program, we added a single listener handler1 three times to the event type foo and then we removed the recently added instance of handler from foo.

Example 3

In the below program, we added a single listener handler up to three instances. first instance with capture: true and the rest two instances with capture: false. Then we called the eventTarget.removeEventListener() method without any {capture:} value. Again we called the eventTarget.removeEventListener() method with {capture: true} value.

const { EventEmitter, listenerCount } = require('node:events');

function handler(event){
};

const eventtarget = new EventTarget();

eventtarget.addEventListener('foo', handler, {capture: true});
eventtarget.addEventListener('foo', handler, {capture: false});
eventtarget.addEvaentListener('foo', handler, {capture: false});

eventtarget.removeEventListener('foo', handler);

eventtarget.removeEventListener('foo', handler, {capture: true});

Output

As per the program the eventtarget.removeEventListener('foo', handler) will remove the recently added instance from the foo event type and the eventtarget.removeEventListener('foo', handler, {capture: true}) will remove the first instance of the handler from the foo event type.

nodejs_events.htm
Advertisements