How To Get JavaScript Conditional Statement To Wait for XMLHTTPResponse?

Discussion in 'Javascript' started by pbd22, Dec 2, 2008.

  1. pbd22

    pbd22 Guest

    Hi.

    It seems my xmlhttp response is taking too long. I have something like
    this:

    try
    {
    if (a === "")
    err += "some error";

    if (b==="")
    err += "some error";

    if (c==="")
    {
    err += "some error";
    }
    else
    {
    // use an xmlhttp request to compare a condition
    // that must be evaluated from the server. the response
    // is true or false.
    }

    }
    catch{}

    finally{

    if (err ==="")
    //submit the form
    else
    //let the user know about the errors.
    }

    THE PROBLEM is that the logic does not publish the response of the
    Ajax request
    because the round-trip takes too long. I have tested this by adding an
    alert. If the alert
    fires and then I click off the alert dialog, the appropriate response
    from the Ajax request
    is in the error list. But, otherwise, it seems like the time it takes
    to respond is omitting the
    response in the error list.

    Is there a way in JavaScript in an If/Else scenario to force getting
    the appropriate response
    from the Ajax request before the next code block is evaluated?

    Thanks in advance.
     
    pbd22, Dec 2, 2008
    #1
    1. Advertising

  2. pbd22

    David Mark Guest

    On Dec 2, 2:52 pm, pbd22 <> wrote:
    > Hi.
    >
    > It seems my xmlhttp response is taking too long. I have something like
    > this:
    >
    > try
    > {
    > if (a === "")
    > err += "some error";
    >
    > if (b==="")
    > err += "some error";
    >
    > if (c==="")
    > {
    > err += "some error";}
    >
    > else
    > {
    > // use an xmlhttp request to compare a condition
    > // that must be evaluated from the server. the response
    > // is true or false.
    >
    > }
    > }
    >
    > catch{}
    >
    > finally{
    >
    > if (err ==="")
    > //submit the form
    > else
    > //let the user know about the errors.
    >
    > }
    >
    > THE PROBLEM is that the logic does not publish the response of the
    > Ajax request
    > because the round-trip takes too long. I have tested this by adding an
    > alert. If the alert
    > fires and then I click off the alert dialog, the appropriate response
    > from the Ajax request
    > is in the error list. But, otherwise, it seems like the time it takes
    > to respond is omitting the
    > response in the error list.
    >
    > Is there a way in JavaScript in an If/Else scenario to force getting
    > the appropriate response
    > from the Ajax request before the next code block is evaluated?
    >
    > Thanks in advance.


    You would have to use a synchronous request, which is a very bad idea
    for a browser script. Change your design to suit an asynchronous
    request.
     
    David Mark, Dec 2, 2008
    #2
    1. Advertising

  3. pbd22

    pbd22 Guest

    On Dec 2, 1:12 pm, David Mark <> wrote:
    > On Dec 2, 2:52 pm, pbd22 <> wrote:
    >
    >
    >
    > > Hi.

    >
    > > It seems my xmlhttp response is taking too long. I have something like
    > > this:

    >
    > > try
    > > {
    > > if (a === "")
    > > err += "some error";

    >
    > > if (b==="")
    > > err += "some error";

    >
    > > if (c==="")
    > > {
    > > err += "some error";}

    >
    > > else
    > > {
    > > // use an xmlhttp request to compare a condition
    > > // that must be evaluated from the server. the response
    > > // is true or false.

    >
    > > }
    > > }

    >
    > > catch{}

    >
    > > finally{

    >
    > > if (err ==="")
    > > //submit the form
    > > else
    > > //let the user know about the errors.

    >
    > > }

    >
    > > THE PROBLEM is that the logic does not publish the response of the
    > > Ajax request
    > > because the round-trip takes too long. I have tested this by adding an
    > > alert. If the alert
    > > fires and then I click off the alert dialog, the appropriate response
    > > from the Ajax request
    > > is in the error list. But, otherwise, it seems like the time it takes
    > > to respond is omitting the
    > > response in the error list.

    >
    > > Is there a way in JavaScript in an If/Else scenario to force getting
    > > the appropriate response
    > > from the Ajax request before the next code block is evaluated?

    >
    > > Thanks in advance.

    >
    > You would have to use a synchronous request, which is a very bad idea
    > for a browser script.  Change your design to suit an asynchronous
    > request.


    OK, thanks. Would you mind providing some pseudo code to give me a
    sense of things?

    Thanks again.
     
    pbd22, Dec 3, 2008
    #3
  4. pbd22

    David Mark Guest

    On Dec 3, 3:00 pm, pbd22 <> wrote:
    > On Dec 2, 1:12 pm, David Mark <> wrote:
    >
    >
    >
    > > On Dec 2, 2:52 pm, pbd22 <> wrote:

    >
    > > > Hi.

    >
    > > > It seems my xmlhttp response is taking too long. I have something like
    > > > this:

    >
    > > > try
    > > > {
    > > > if (a === "")
    > > > err += "some error";

    >
    > > > if (b==="")
    > > > err += "some error";

    >
    > > > if (c==="")
    > > > {
    > > > err += "some error";}

    >
    > > > else
    > > > {
    > > > // use an xmlhttp request to compare a condition
    > > > // that must be evaluated from the server. the response
    > > > // is true or false.

    >
    > > > }
    > > > }

    >
    > > > catch{}

    >
    > > > finally{

    >
    > > > if (err ==="")
    > > > //submit the form
    > > > else
    > > > //let the user know about the errors.

    >
    > > > }

    >
    > > > THE PROBLEM is that the logic does not publish the response of the
    > > > Ajax request
    > > > because the round-trip takes too long. I have tested this by adding an
    > > > alert. If the alert
    > > > fires and then I click off the alert dialog, the appropriate response
    > > > from the Ajax request
    > > > is in the error list. But, otherwise, it seems like the time it takes
    > > > to respond is omitting the
    > > > response in the error list.

    >
    > > > Is there a way in JavaScript in an If/Else scenario to force getting
    > > > the appropriate response
    > > > from the Ajax request before the next code block is evaluated?

    >
    > > > Thanks in advance.

    >
    > > You would have to use a synchronous request, which is a very bad idea
    > > for a browser script.  Change your design to suit an asynchronous
    > > request.

    >
    > OK, thanks. Would you mind providing some pseudo code to give me a
    > sense of things?
    >
    > Thanks again.


    See the FAQ notes for an example.
     
    David Mark, Dec 3, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    807
  2. Alec S.
    Replies:
    10
    Views:
    10,157
    Alec S.
    Apr 16, 2005
  3. Huey

    How to make "fork/wait" to WAIT longer?

    Huey, Mar 1, 2004, in forum: C Programming
    Replies:
    1
    Views:
    1,968
    -berlin.de
    Mar 1, 2004
  4. bthumber
    Replies:
    5
    Views:
    421
    Alexey Smirnov
    Jan 29, 2009
  5. Sharat Mohan
    Replies:
    0
    Views:
    267
    Sharat Mohan
    Feb 13, 2009
Loading...

Share This Page