this post was submitted on 12 Sep 2023
20 points (85.7% liked)

Programming

17020 readers
233 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
 

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.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 12 points 1 year ago* (last edited 1 year ago)

For your own sanity, please use a formatter for your IDE. This will also help when others (and you) read the code, as indentation is a convenience for understanding program flow. From what I see:

  • Your enable and disable functions are never called for this portion of code
  • You use a possibly undeclared enabled variable, if so it never passes scopes between the handleClick and animation methods
  • You do not use any callback or await for invoke or updateCurrentBox, causing all the code after either to immediately run. As a result, enabled is never false, since it just instantly flips back to true. I'm not sure what library invoke is from, but there should be a callback or the function returns a Promise which can be awaited.