title: Wait for a user event
body: <p>I'm developing a javascript class (with jQuery) to create custom popup, but I can not figure out how to "pause" the script to wait for user response (click on the OK button or cancel)</p>
  
  <p>the class is like this:</p>
  
  <pre><code>function Popup()
  {
  }
  
  Popup.Show = function(text)
  {
      var _response;
      /* code to draw the popup elements*/
  
      $("#button-ok").on("click",function()
      {
          _response = "ok!"
      }
  
      return _response
  }
  </code></pre>
  
  <p>if, for example, use it in an alert, the method always return "undefined", because it does not wait for user click</p>
  
  <pre><code>alert(Popup.Show("hello"))
  //return undefined before the popup appears
  </code></pre>
  
  <p>I tried to use a while loop like this:</p>
  
  <pre><code>Popup.Show = function(text)
  {
      var _response;
      var _wait = true;
  
      /* code to draw the window */
  
      $("#button-ok").on("click",function()
      {
          _wait = false;
          _response = "ok!"
      }
  
      while(_wait){};
  
      return _response
  }
  </code></pre>
  
  <p>but does not work (as I expected)</p>
  
  <p>Then, how do I wait for the user click?</p>
  
  <p>Thanks to all</p>