سؤال

I need to make this function have the ability to have an add event listener and remove event listener

canvas2 was $('canvas') but i changed it to make it easier to use

canvas2.mousedown(function(e){handleMouseDown(e);});

because this does not have an event listener

هل كانت مفيدة؟

المحلول

Wild guess: You want

canvas2.on("mousedown", handleMouseDown);
…
canvas2.off("mousedown", handleMouseDown);

With the unnecessary anonymous function you did not have a reference to the actual passed listener, so you could not remove it any more.

when the function has been used the function needs to be disengaged

You might also consider using .one("mousedown", handleMouseDown), then.

نصائح أخرى

You need to say either

canvas2.onmousedown=function(e){
     handleMouseDown(e);
});

or

canvas2.addEventListener('mousedown',function(e){
     handleMouseDown(e);
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top