Wait for a user event

29,224

JavaScript is asynchronous, you cannot "pauses" execution. Moreover, while javascript is running the entire user interface freezes, so the user cannot click the button.

Once you have registered the callback to the click event, the program continues its execution. The following lines in your script

    _wait = false;
    _response = "ok!"

will never get executed until the Popup.Show function returns. Perhaps this Nettuts article about javascript event programming will help you understand the paradigm.

Here is a try to fix your code :

Popup.Show = function(text)
{
  /* code to draw the window */

  // bind click event
  var myPopup = this;
  $("#button-ok").on("click",function()
  {
    // this code will be executed after Popup.Show has return
    myPopup.response = "ok!"
    myPopup.Close();
  }
}
Share:
29,224
Luca
Author by

Luca

Updated on November 11, 2020

Comments

  • Luca
    Luca over 3 years

    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)

    the class is like this:

    function Popup()
    {
    }
    
    Popup.Show = function(text)
    {
        var _response;
        /* code to draw the popup elements*/
    
        $("#button-ok").on("click",function()
        {
            _response = "ok!"
        }
    
        return _response
    }
    

    if, for example, use it in an alert, the method always return "undefined", because it does not wait for user click

    alert(Popup.Show("hello"))
    //return undefined before the popup appears
    

    I tried to use a while loop like this:

    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
    }
    

    but does not work (as I expected)

    Then, how do I wait for the user click?

    Thanks to all

  • Luca
    Luca over 10 years
    Yes, I knew that the events are asynchronous, the problem was precisely this. What can be the solution to get around this and make sure that the function returns an user response?
  • Luca
    Luca over 10 years
    thank you, but the real problem is waiting for the user event before returning value. How can I do?
  • Fabien Quatravaux
    Fabien Quatravaux over 10 years
    This in not possible. The function will always return before the user can click the button. You have to change your code to take that into account.
  • Luca
    Luca over 10 years
    So I can't replicate this: alert(prompt())? In this case prompt() waits for user confirmation, or not?
  • jHilscher
    jHilscher over 10 years
    That is not possible, you have to restructure your code, to make use of the callback. Like this: link
  • artomason
    artomason about 5 years
    JavaScript is not asynchronous; there are functions within JavaScript such as making AJAX calls that create asynchronous behavior, but the base language itself is synchronous.