L
L
LaraG2020-08-04 01:14:59
Node.js
LaraG, 2020-08-04 01:14:59

Should the function be executed only once when the button is clicked?

Here is such a function:

socket.on('add mess', function(data) {
        $all_users.append("<div>"+ data.name +  "</div>");
      });

The user can click on the button many times, but if the message was published once, it will not be repeated again
. How to display it?
What should this function be wrapped in?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2020-08-04
@yarkov

The algorithm is this:

  1. We move the code that should work out into a separate function, for exampleFunctionName
  2. At the end of the function we write something like:button.removeEventListener("click", FunctionName)
  3. We hang the handler on the button:button.addEventListener("click", FunctionName)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question