Skip to content

Conversation

@andyforever
Copy link

to ensure element eventListener bound once

@andyforever
Copy link
Author

When a clipboard button is in loop, click eventListener will be bound mutiple times and onsuccess event be triggered multiple times.

@andyforever andyforever changed the title Update delegate.js to ensure element eventListener bound once Oct 23, 2018
LostInBrittany added a commit to LostInBrittany/delegate that referenced this pull request Nov 5, 2018
@fregante
Copy link
Contributor

fregante commented Dec 19, 2018

This doesn't work for two reasons:

  • addEventListener actually already only attaches a listener only once, automatically avoiding identical type/listeners/options being repeatedly attached
  • listenerFn is generated on the fly so it's always different from the previously added one, so you can't remove it

You can test the first reason by running this code in your browser and then clicking anywhere: the log only shows once because the listener (console.log) is only added once.

document.addEventListener('click', console.log);
document.addEventListener('click', console.log);

@fregante
Copy link
Contributor

fregante commented Mar 7, 2019

I took care of this in delegate-it. I forked delegate because old PRs weren't merged and took the opportunity to modernize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants