how to turn events on and off in Javascript - eviltoast

Hello, so I am experiencing a problem in my javascript app where I cannot stop a user spamming a button. The app is used to draw three random numbers from a range. when a button is clicked, a number is drawn and a special animation plays, then the next number can be drawn. I am having trouble preventing the user from spamming the draw button. So far I have tried using a local variable called enabled which is set to false when the animation is playing and checked to be true before the actual animation function executes (its set back to false after it executes). I have also tried to remove and add the event listener of the button dynamically when it is pressed. Is there any way you guys think I can solve the issue? Thank you. The code should be in an image with the post.

  • macniel@feddit.de
    link
    fedilink
    arrow-up
    8
    ·
    1 year ago

    You need a reference to your Eventhandler. So instead of just giving an anonymous callback function to your addEventHandler store it in a variable instead. Then your can call removeEventHandler with the same event type and the variable of your Eventhandler you want to remove.