
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
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.