-4

I am trying to add a delay to a loop where the prompt asks the user a question, and repeatedly asks the same question, until the correct answer, "yes" is entered. I then would like the loop to repeat after about a minute or so. I tried to look this up elsewhere, but can't seem to figure out how to properly input setTimeout or setInterval to my code. Any help would be appreciated

code:

`var answer= prompt(" (yes/no)");
while(answer.indexOf("yes") === -1) {
var answer= prompt("type yes to continue");
}
alert("Welcome!")`

1 Answers1

0

There is no need for a while loop or async and await. Just create a function and have the timeout call the function.

function ask() {
  const answer = window.prompt("yes");
  if (answer === "yes") {
    alert("okay");
  } else {
    window.setTimeout(ask, 2000);
  }
}

ask()
epascarello
  • 204,599
  • 20
  • 195
  • 236