0

currently my code works with this method:

canvas.addEventListener('mousemove', function(event) {....},false)

now I want to separate the function, make own function. like:

  canvas.addEventListener('mousemove', myFunction(1), false)
  canvas.addEventListener('mousedown', myFunction(0), false)

  function myFunction (event, separator) {..}

but its not working

please help, thanks

1 Answer 1

1

The second parameter for addEventListener is supposed to be a function reference (i.e. an object representing a function to be called in case of an event).

But you provide the result of the call to myFunction() as a parameter, which would only work if myFunction returned a function reference (i.e. if myFunction were a function factory).

function myFunction(myEvent, separator) {
   if(separator==0)
      return function(myEvent) {
         // your code goes here
      }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.