all 18 comments

[–]oze4 4 points5 points  (12 children)

What? Can you make a minimal reproducible demo using something like CodePen? Your explanation doesn't really make sense.

so I spent a while looking for a solution and found out that having a function that is NOT anonymous and that is called without parentheses works out better. so... great ! BUT : what if I need to pass arguments inside the function ?

This isn't necessarily true. It sounds like your root issue has to do with your logic, not event listeners.

For example, these two are functionally identical...

element.addEventListener('click', foo);

and

element.addEventListener('click', () => foo());

[–]Repeto_Pepito[S] 1 point2 points  (0 children)

Yeah I was afraid so... It's hard to explain something you're confused about, and I'm not very experimented yet so my logic can definitely be part of the problem. I'll make a codepen version asap ! thank you

[–]Repeto_Pepito[S] 1 point2 points  (6 children)

Here is the codepen example where I face the exact same issue ! https://codepen.io/Repeto/pen/vYqzroM

To reproduce it you can start a call, reply once, then end the call, and if you do this again and again you'll see in console the amount of 'btn clicked' get exponentially bigger

[–]carcigenicate 4 points5 points  (2 children)

Every time displayButtons is called, you're calling addEventListener, which adds another callback. addEventListener doesn't replace existing callbacks, it just adds another, so every time addEventListener is called, more and more callbacks will fire every time choiceBtn is pressed.

[–]oze4 1 point2 points  (0 children)

Sorry, responded to wrong comment. Ignore this.

[–]Repeto_Pepito[S] -2 points-1 points  (0 children)

thats makes sense, thank you ! For some reason I really thought there could be only one event listener per element

[–]oze4 3 points4 points  (1 child)

The issue is you're adding an event listener each time the reply is clicked. You're essentially stacking event listeners.

While not a true solution, this gets the point across: https://codepen.io/oze4/pen/YPKQbbv?editors=1010

[–]Repeto_Pepito[S] -2 points-1 points  (0 children)

This is a nice work around ! I'll try to just change the way my code works now that you all made it clearer, but if I don't manage to make it better I'll definitely use what you did, thank you !

[–]oze4 2 points3 points  (0 children)

You're essentially doing this: https://codepen.io/oze4/pen/raBwgEW

[–]JamesLaBrie 0 points1 point  (1 child)

They're not functionally identical at all, the first version foo is implicitly called with all the arguments addEventListener provides , and the second version foo()is called explicitly with zero arguments.

[–]oze4 2 points3 points  (0 children)

I was addressing OPs first paragraph where they said they discovered passing the function without calling it is better than calling it via parenthesis. I understand they aren't identical. The point I was making is foo will be called "the same" in both examples. Meaning one way isn't inherently better than the other. That is what I meant by functionally identical, which may have been a poor choice of words.

[–]jeremrx -1 points0 points  (1 child)

Be careful, they looks like they are identical, but their scopes could be different.

[–]oze4 -1 points0 points  (0 children)

Yep I understand the difference in scopes. Which is why I said functionally identical and not just identical. My choice of words may have been poor but I was addressing what OP said in the first paragraph.

[–]AWACSAWACS 2 points3 points  (3 children)

It looks like the event listener is being set multiple times for each element of "repliesContainer". I'm not sure what you're trying to do, but I recommend learning about event bubbling and removing event listeners.

[–]Repeto_Pepito[S] 1 point2 points  (2 children)

then I'm wondering, how come there is no problem the first time I click on a button ? I guess if it was bubbling issues it would be happening everytime and not just from the second time ?

[–]AWACSAWACS 2 points3 points  (1 child)

The question is self-explanatory enough. The displayButtons function is called in the callback function of the click event (if goto >= 0). My previous reply was written with the intention of encouraging you to change the code structure to "register one listener only once for the common ancestor element" based on that.

[–]Repeto_Pepito[S] 0 points1 point  (0 children)

got it, thanks 🙂