delegate icon indicating copy to clipboard operation
delegate copied to clipboard

to ensure element eventListener bound once

Open andyforever opened this issue 7 years ago • 3 comments

to ensure element eventListener bound once

andyforever avatar Oct 23 '18 08:10 andyforever

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

andyforever avatar Oct 23 '18 08:10 andyforever

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 avatar Dec 19 '18 09:12 fregante

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

fregante avatar Mar 07 '19 15:03 fregante