how does ajax affect execution of code

Discussion in 'Javascript' started by Andrew Poulos, Aug 18, 2008.

  1. If I have code that looks like this

    ajax = function(str) {
    var val = "";
    // do some stuff here
    return val;
    };

    var foo = ajax("string");


    Does execution continue before a value is assigned to foo?

    Andrew Poulos
    Andrew Poulos, Aug 18, 2008
    #1
    1. Advertising

  2. Andrew Poulos wrote:

    > If I have code that looks like this
    >
    > ajax = function(str) {
    > var val = "";
    > // do some stuff here
    > return val;
    > };
    >
    > var foo = ajax("string");


    This doesn't seem to have anthing to do with Ajax aside from your choice of
    variable name.

    > Does execution continue before a value is assigned to foo?


    Continue from where?

    The current code assigns a function to a variable (declared without var)
    called 'ajax'. It immediately calls that function passing in the
    argument "string". Next it assigns an empty string to a variable 'val', and
    then returns that variable (assinging it to a variable called 'foo').

    There is nothing odd about the timing of this.

    (If XMLHttpRequest was involved, then the timing might get interesting, but
    it isn't).


    --
    David Dorward
    http://dorward.me.uk/
    http://blog.dorward.me.uk/
    David Dorward, Aug 18, 2008
    #2
    1. Advertising

  3. Andrew Poulos meinte:
    > If I have code that looks like this
    >
    > ajax = function(str) {


    var ajax = ... might be smarter.

    > var val = "";
    > // do some stuff here
    > return val;
    > };
    >
    > var foo = ajax("string");
    >
    >
    > Does execution continue before a value is assigned to foo?


    In this very example? No.

    Do you know what ajax or - to be more precise - "XHR" is?

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Aug 18, 2008
    #3
  4. Gregor Kofler wrote:
    > Andrew Poulos meinte:
    >> If I have code that looks like this
    >>
    >> ajax = function(str) {

    >
    > var ajax = ... might be smarter.


    If ajax was not declared earlier.

    >> var val = "";
    >> // do some stuff here
    >> return val;
    >> };
    >>
    >> var foo = ajax("string");
    >>
    >>
    >> Does execution continue before a value is assigned to foo?

    >
    > In this very example? No.
    >
    > Do you know what ajax or - to be more precise - "XHR" is?
    >

    Sorry I was saving space by only including what I thought was relevant
    code. Please assume that within the function assigned to the variable
    "ajax" are all the relevant XMLHttpRequest bits and pieces.

    If I reword the question to, while we're waiting for something to return
    from an XMLHttpRequest does execution of the javascript code continue?

    If it does what happens when the event onreadystatechange has a
    readyState equal to 4? Does execution stop whenever its up to and run
    the code under the onreadystatechange function?

    Andrew Poulos
    Andrew Poulos, Aug 18, 2008
    #4
  5. Andrew Poulos

    Henry Guest

    On Aug 18, 10:18 am, Andrew Poulos wrote:
    > Gregor Kofler wrote:
    >> Andrew Poulos meinte:
    >>> If I have code that looks like this

    >
    >>> ajax = function(str) {

    >
    >> var ajax = ... might be smarter.

    >
    > If ajax was not declared earlier.
    >
    >>> var val = "";
    >>> // do some stuff here
    >>> return val;
    >>> };

    >
    >>> var foo = ajax("string");

    >
    >>> Does execution continue before a value is assigned to foo?

    >
    >> In this very example? No.

    >
    >> Do you know what ajax or - to be more precise - "XHR" is?

    >
    > Sorry I was saving space by only including what I thought was
    > relevant code. Please assume that within the function assigned
    > to the variable "ajax" are all the relevant XMLHttpRequest
    > bits and pieces.


    So should we be assuming that the XML HTML requests are being made
    synchronously or asynchronously?

    > If I reword the question to, while we're waiting for something
    > to return from an XMLHttpRequest does execution of the javascript
    > code continue?


    To which the answer is yes or no, depending on how you make the XML
    HTTP request.

    > If it does what happens when the event onreadystatechange
    > has a readyState equal to 4?


    If anything is going to happen when the readyState equals 4 then "it"
    must do whatever is done.

    > Does execution stop whenever its up to and run
    > the code under the onreadystatechange function?


    Execution stops and code runs? That seems a bit contradictory. Are you
    asking whether events interrupt already executing code? They don't.
    Henry, Aug 18, 2008
    #5
  6. Andrew Poulos meinte:

    > Sorry I was saving space by only including what I thought was relevant
    > code. Please assume that within the function assigned to the variable
    > "ajax" are all the relevant XMLHttpRequest bits and pieces.


    Ah a guessing game. Synchronous calls or asynchronous? In the latter
    case: Once the request is sent, the skript moves on - therefore you need
    a callback function to access the information returned from the server.
    >
    > If I reword the question to, while we're waiting for something to return
    > from an XMLHttpRequest does execution of the javascript code continue?


    If I'd have to wait: What would this XHR fuss all be good for? You (or
    rather your script) normally doesn't wait. See above.

    > If it does what happens when the event onreadystatechange has a
    > readyState equal to 4? Does execution stop whenever its up to and run
    > the code under the onreadystatechange function?


    Depends on what you mean by "execution stops". If it helps: JS is single
    threaded.

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Aug 18, 2008
    #6
  7. Gregor Kofler wrote:
    > Andrew Poulos meinte:
    >
    >> Sorry I was saving space by only including what I thought was relevant
    >> code. Please assume that within the function assigned to the variable
    >> "ajax" are all the relevant XMLHttpRequest bits and pieces.

    >
    > Ah a guessing game. Synchronous calls or asynchronous? In the latter
    > case: Once the request is sent, the script moves on - therefore you need
    > a callback function to access the information returned from the server.


    Thanks I didn't know they could be synchronous.

    >> If I reword the question to, while we're waiting for something to
    >> return from an XMLHttpRequest does execution of the javascript code
    >> continue?

    >
    > If I'd have to wait: What would this XHR fuss all be good for? You (or
    > rather your script) normally doesn't wait. See above.
    >
    >> If it does what happens when the event onreadystatechange has a
    >> readyState equal to 4? Does execution stop whenever its up to and run
    >> the code under the onreadystatechange function?

    >
    > Depends on what you mean by "execution stops". If it helps: JS is single
    > threaded.


    Sorry, its obvious that I'm having trouble understanding ajax concepts.

    I can get a value returned by a server using ajax. How do you handle the
    situation where subsequent ajax calls are dependent upon the value
    returned from previous calls? As I don't know when those values will return.

    Andrew Poulos
    Andrew Poulos, Aug 19, 2008
    #7
  8. Andrew Poulos meinte:

    > I can get a value returned by a server using ajax. How do you handle the
    > situation where subsequent ajax calls are dependent upon the value
    > returned from previous calls? As I don't know when those values will
    > return.


    Those subsequent XHR calls are triggered in the response callback function.

    Pseudo:

    xhr.onreadystatechange = function() {
    if (xhr.readystate === 4) {
    foo(xhr.responseText);
    }
    }
    var foo = function(responseText) {
    trigger another XHR request;
    }


    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Aug 19, 2008
    #8
    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. mmike
    Replies:
    0
    Views:
    545
    mmike
    May 19, 2004
  2. CB
    Replies:
    1
    Views:
    129
    Bullschmidt
    Feb 23, 2004
  3. RLC
    Replies:
    11
    Views:
    412
    Henrik Faber
    Feb 9, 2012
  4. RLC
    Replies:
    6
    Views:
    425
  5. Joe keane
    Replies:
    4
    Views:
    220
    Henrik Faber
    Feb 9, 2012
Loading...

Share This Page