question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Events: listener without context can be added twice

See original GitHub issue
var m = new L.Marker();
var x = function() {console.log("event fired")};

/* register without context */
m.on("test", x);
m.on("test", x);

/* fires twice */
m.fire("test");

/* register with context */
context = {}
m.on("test2", x, context);
m.on("test2", x, context);

/* fires once*/
m.fire("test2");

https://playground-leaflet.rhcloud.com/wum/1/edit?html,js,output

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
yohanbonifacecommented, Jul 1, 2016

Addendum: If you supply the second listener with two different contexts it also fires twice (once for each context).

We had a small chat about this, and it seems it’s a feature, not a bug 😉 Basically, this is what you are doing when you do something like:

this._map.on('click', this._onClick, layer1)
this._map.on('click', this._onClick, layer2)

So what needs to be unique is the combination of listener + context.

0reactions
fab1ancommented, Jul 17, 2016

This is fixed now, is it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rebind JavaScript events and addEventListener fires twice
The .addEventListener method ensures that the same function reference won't be bound more than once to the same element/event/captures ...
Read more >
How to ensure an event listener is only fired once in JavaScript
We can pass an object as an argument to the addEventListener method and specify that the event is only handled once. This is...
Read more >
event listener pattern in api - what should adding the same ...
If you allow the same listener to be added twice, it should be clear which of the two listener entries is going to...
Read more >
Event Listeners being called twice. - Google Groups
I'm having an issue where my eventListeners are all called twice for one give event emission - I believe the listeners are registered...
Read more >
Introduction to browser events - The Modern JavaScript Tutorial
All DOM nodes generate such signals (but events are not limited to ... To react on events we can assign a handler –...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found