title: JavaScript pausing execution of function to wait for user input
body: <p>I'm trying to make a sort of game using the HTML5 canvas, JavaScript and XML.
  The idea is that you can make a quiz by putting questions and answers in an XML file.
  I wrote a main loop that loops through all the questions, poses them and checks the correctness of the answer.
  For now I'm simply using alerts and dialog boxes to answer the questions
  The problem is that my main loop is one big interconnected whole that walks through the entire game from beginning to end, and instead of having alert boxes posing questions and dialog boxes to answer, immediately after one another, I want some user interaction.
  The answers to the questions appear on boxes at the bottom of the screen, and the user gets to control a crane to pick the right answer.
  Here's the snippet of code from the main loop I'm stuck on:</p>
  
  <pre><code>answer = loadQuestion(i);
              if (answer == "correct") {
                  // answered correctly, update scoreArray and load next question
                  scoreArray[currentQuestion] = "correct";
                  // show 'next'-button and wait for press to continue
  
              } else {
                  // answered incorrectly again, update scoreArray and load next question
                  scoreArray[currentQuestion] = "error";
                  // show 'next'-button and wait for press to continue
  
              }
  </code></pre>
  
  <p>As you can see I'm calling loadQuestion, which instantly loads the question, shows the possible answers, and for now immediately throws a dialog box where you can type the answer.
  This answer is returned and validated.</p>
  
  <p>I have already programmed the controls of the crane, the user can already pick up a box with it.
  But because I'm calling loadQuestion and expecting it to return a value, this doesn't work.
  How do I make my main loop "pause" until an answer has been given by the player using the crane, and then proceed?
  I already tried making the answer a global variable, and just having an empty <em>while answer == ""</em> to keep the function busy doing nothing until answer gets a value, but this just freezes the script.
  I also messed around with intervals in an attempt to monitor the status of the answer variable, and clear the interval and return the value when this happens, but that simply returns false since the function completes without immediately returning a value.</p>