For a project I'm making a board game using HTML and JavaScript, and I'm fairly new to it. I need the main function to wait for the player to click "roll" and then return the result as a variable so it can be used for other functions.
How do I force JavaScript to wait for the button to be clicked and how can I then get a variable from the script that runs?
The HTML of the button looks like this:
<input type = "button" id="roll" value = "roll">
How the JS might look:
function makeNumber(){
//returns number >=1 <=6
}
function main(){
while (win ! true){
//here it should wait for the click event
document.getElementById("roll").addEventListener("click",
makeNumber());
var diceResult; // here id like to save the result from the
makeNumber function above
doSomething(diceResult);
}
}
I've also tried this other method, but have the exact same problem:
document.getElementById("foo").onclick = function() {myFunction()};