Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #40465

Re: removing event listener

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: removing event listener
Date 2021-10-12 22:55 +0200
Organization PointedEars Software (PES)
Message-ID <2529056.k3LOHGUjKi@PointedEars.de> (permalink)
References <8audnTTCjcPf5P78nZ2dnUU7-KnNnZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> Interaction = function() {

This should be

  let Interaction = function () {

>   ...
> };
> 
> Interaction.prototype.action = function() {
>    let me = this;

This workaround is not necessary if you are using Function.prototype.bind() 
anyway, although referring to the “this” value by “me” instead of “this” 
might be more efficient.

>    ...
>    document.addEventListener('keydown', me.action.bind(me));

Change this line to

  let keyDown = me.keyDown = me.action.bind(me);
  document.addEventListener('keydown', keyDown);

or something similar,

> };
> 
> Interaction.prototype.reaction = function() {
>    let me = this;
>    ...
>    document.removeEventListener('keydown', me.action.bind(me));

and this line to

  document.removeEventListener('keydown', me.keyDown);

respectively.

You should consider whether the document object really is the right object 
to listen to the event, or if, for example, the object referred to by 
“document.body” is more appropriate.  In general, you should add event 
listeners for bubbling events as deep in the DOM tree as is feasible,
to avoid unwanted side effects.

> };
> 
> but when 'reaction' gets called the 'keydown' event listener doesn't get
> removed. How can I remove the event listener within 'reaction'?

It is perhaps a good idea to define a method that handles the “keydown” 
event (listener) exclusively for the instance that inherits that method.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

removing event listener Andrew Poulos <ap_prog@hotmail.com> - 2021-10-11 10:37 +1100
  Re: removing event listener Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-11 16:42 +0100
    Re: removing event listener Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-10-12 22:43 +0200
  Re: removing event listener Julio Di Egidio <julio@diegidio.name> - 2021-10-11 09:46 -0700
    Re: removing event listener JJ <jj4public@gmail.com> - 2021-10-12 22:21 +0700
      Re: removing event listener Julio Di Egidio <julio@diegidio.name> - 2021-10-12 09:57 -0700
    Re: removing event listener John Harris <niam@jghnorth.org.uk.invalid> - 2021-10-12 18:13 +0100
      Re: removing event listener Julio Di Egidio <julio@diegidio.name> - 2021-10-12 16:05 -0700
        Re: removing event listener John Harris <niam@jghnorth.org.uk.invalid> - 2021-10-13 18:10 +0100
    Re: removing event listener John Harris <niam@jghnorth.org.uk.invalid> - 2021-10-12 18:15 +0100
  Re: removing event listener Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-10-12 22:55 +0200

csiph-web