
- 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
NodeJS - emitter.off() Method
The emitter.off() method will remove the specified listener function from the listener array of the event named eventName.
This method will remove the most recently added instance of a listener function from the listener array. If in case one listener is added multiple times to the listener array of the event named eventName, then the emitter.off() method should be called multiple times to remove each instance from the listener array.
This method belongs to Eventemitter class is an inbuilt class of node:events module.
Syntax
Following is the syntax of the NodeJs emitter.off() method −
emitter.off(eventName, listener)
Parameters
This method accepts two parameters as described below.
- eventName: This is the first parameter of the method and it holds the name of the event. It can be either string or a symbol.
- listener: This parameter holds the callback function.
Return value
This method returns a reference to the EventEmitter so that calls can be chained.
Example 1
The following is the basic example of the NodeJs emitter.off() Method.
Here, we created a function f1() with a message inside it by calling the emitter.on() method with an eventName (event) passed as the first parameter and we passed the (f1) to the listener function parameter of the method. Thus the function is added to the listener array. Then we called myEmitter.off() method with the eventName (event) and f1() function. So when we compile and run the program myEmitter.off() method will remove the listener function f1() of event named event.
const EventEmitter = require('node:events'); const myEvent = new EventEmitter(); function one(){ console('Welcome to Earth'); }; myEvent.on('event', one); console.log(myEvent.eventNames());
Output
The above program produces the following output −
Program did not output anything!
Example 2
Following is the program passing a similar combination of eventName and listener function to the myEmitter.on() method. This will result in the listener function being added to the listener array and called multiple times. So that the function f1() is added four times to the listener array. Then we called myEmitter.off() method two times with the same combination of the eventName and listener function which we passed in myEmitter.on() method. So only two instances of listener function f1() will be removed.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ console.log('Welcome to tutorialspoint'); } myEmitter.on('event', f1); myEmitter.on('event', f1); myEmitter.on('event', f1); myEmitter.on('event', f1); myEmitter.off('event', f1); myEmitter.off('event', f1); myEmitter.emit('event');
Output
The above program produces the following output −
Welcome to tutorialspoint Welcome to tutorialspoint
Example 3
In this program, we added a single listener function (f1) multiple times for a single event (event). Then we called the myEmitter.off() method with an event named event and listener f1(). If we compile and run the program, the off() method will remove the most recently added instance. Here, myEmitter.on(event) will be removed.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ console.log('Bye'); } myEmitter.once('event', f1); myEmitter.addListener('event', f1); myEmitter.on('event', f1); myEmitter.off('event', f1); myEmitter.emit('event');
Output
The above program produces the following output −
Bye Bye