Home

addListener

AddListener is a method used in event-driven programming to register a function, or listener, to be invoked when a specified event occurs. The listener typically receives an event object or data and runs in response to the event. The API commonly accepts the event name or type and a callback function. Many frameworks allow multiple listeners for a single event and invoke them in a defined order when the event is emitted or dispatched. Some environments also provide a once variant that automatically removes the listener after the first invocation, and a separate removeListener or off function to detach listeners.

In Node.js, the EventEmitter class defines addListener as an alias of on; both register a listener for

In web browsers, the standard approach is addEventListener, which serves the same purpose for DOM events. Some

See also: on, removeListener, off, once.

a
named
event.
The
listener
remains
active
until
it
is
explicitly
removed
with
removeListener
or
off.
The
addition
operation
is
typically
side-effectful,
and
the
return
value,
if
any,
is
usually
the
emitter
itself
to
facilitate
chaining.
libraries
expose
an
addListener
method
to
provide
a
uniform
API
across
environments.
In
all
cases,
it
is
important
to
remove
listeners
when
they
are
no
longer
needed
to
avoid
memory
leaks,
especially
in
long-running
applications
or
single-page
apps.