Skip to content Skip to sidebar Skip to footer

Socket.io Returning Multiple Times For One Event

Before this is flagged as a duplicate, I have checked the majority of other posts and solutions, to no avail. If anyone wants to double check, here they are: 1) Socket.io Multiple

Solution 1:

You're attaching your event handler again every time the button is clicked. You have socket.on inside the click handler; that method attaches a new handler -- that is, each time it's run, it adds the specified function to the end of the list of functions to run when the event fires. So yes, every time you click the button, you're adding the function to the end of the list and it'll run once for every time it was added. (More accurately, since you're using an anonymous function there, it's creating a new function every time you click the button and adding it to the event handler list to be run when the event fires.)

You should only be attaching event handlers once, for instance just after creating the socket, not on every click.

Post a Comment for "Socket.io Returning Multiple Times For One Event"