Skip to content
Advertisement

How can I wait for a click to be executed?

I am trying to figure out how I can update the playerValue for my Rock Scissor Paper game. I have a function that registers a button click and should then update the playerValue accordingly(1,2 or three depending on which button was clicked). The problem is, when I call the function, the playerValue remains 0 and I don’t know what I need to change to fix that. playerValue itself is defined at the very beginning of my file. It starts out as 0.

Here is my JavaScript code(or at least the relevant part of it):

JavaScript

This here is where the playerValue is meant to be used. The playerValue is always 0. I think that is because the player_choose_value() function does not wait for the click event to happen. So the function is executed but the user does not have the chance to actually click a button, so it stays 0:

JavaScript

I was wondering how I could add a “wait for one of the three buttons to be clicked” functionality?

Advertisement

Answer

In your case, player_choose_value doesn’t wait until the player has actually picked a value.

You could probably do this using async await/promises:

JavaScript
Advertisement