I'm new too JavaScript and am having trouble with async. I have a webpage with two buttons, If the yes button is pressed it will do some code and if the no button is pressed it will do different code but regardless of which one is pressed it will continue on in the function. For Example
function mainloop(){
document.getElementById("buttonyes").addEventListener("click", () => {
/* do some code */
})
document.getElementById("buttonno").addEventListener("click", () => {
/* do some different code */
})
/* wait for either button to be pressed and then continue with code */
console.log("yay waiting for stuff")
}
I believe the solution to this is promises and creating other functions to handle the buttons; however, tutorials I've seen only show one function solutions and if I understand correctly the EventListener when activated is using another function all to itself. I've come over from C and all this object properties asynchronization stuff is throwing me for a loop.
I'd really love a way to keep it all in the same main function as this challenge specifically called for me to use async/await on this damned buttons.
console.log
in a seperate function than call it at the end of both the first and last event handlermainloop()
called multiple times? Can the user press these buttons only once, or many times?addEventListener
will remain active once you set it. If you want to make the buttons "unclickable" you coulddisable
them with CSS, or you could remove the listener withremoveEventListener
, or you could keep track of the state with a variable. For example,textDisplayed = true
.