I Have this
Interaction = function() {
...
};
Interaction.prototype.action = function() {
let me = this;
...
document.addEventListener('keydown', me.action.bind(me));
};
Interaction.prototype.reaction = function() {
let me = this;
...
document.removeEventListener('keydown', me.action.bind(me));
};
but when 'reaction' gets called the 'keydown' event listener doesn't
get removed. How can I remove the event listener within 'reaction'?
Interaction.prototype.action = function() {
let me = this;
...
document.addEventListener('keydown', me.action.bind(me));
};
Also, Ben is incorrect: the match must be lexical, not by reference (JS is
a functional language...).
On Mon, 11 Oct 2021 09:46:31 -0700 (PDT), Julio Di Egidio wrote:
Also, Ben is incorrect: the match must be lexical, not by reference (JS is a functional language...).
No. Ben is correct.
As long any of the argument is different when passed to `removeEventListener()` than when passed to `addEventListener()` (i.e. not exact match of argument set), the event handler will never be removed.
The only exception is when the third argument (the options) is an object
when adding an event listener. The object reference doesn't need to match when the event listener is to be removed.
(JS is a functional language...).
Interaction = function() {
...
};
Interaction.prototype.action = function() {
let me = this;
...
document.addEventListener('keydown', me.action.bind(me));
};
Interaction.prototype.reaction = function() {
let me = this;
...
document.removeEventListener('keydown', me.action.bind(me));
};
but when 'reaction' gets called the 'keydown' event listener doesn't get removed. How can I remove the event listener within 'reaction'?
bind creates a new anonymous function but you can only remove an event function by passing in the exact same function you passed when calling addEventListener. You'll have to store that function value somewhere.
On 11/10/2021 17:46, Julio Di Egidio wrote:
<snip>
(JS is a functional language...).The ECMAScript standard, ECMA-262 11th Edition/June 2020, doesn't say
that, so you can't rely on that to tell you the way a program will work.
On Tuesday, 12 October 2021 at 19:13:39 UTC+2, John Harris wrote:<snip>
On 11/10/2021 17:46, Julio Di Egidio wrote:
<snip>
(JS is a functional language...).The ECMAScript standard, ECMA-262 11th Edition/June 2020, doesn't say
that, so you can't rely on that to tell you the way a program will work.
The case in point was "the match must be lexical, not by reference", which of course you can't even parse even less understand the import of, and which of course is the thing that is documented, just not in those high-level words.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 469 |
Nodes: | 16 (2 / 14) |
Uptime: | 44:46:01 |
Calls: | 9,449 |
Calls today: | 6 |
Files: | 13,596 |
Messages: | 6,112,014 |
Posted today: | 1 |