how does ajax affect execution of code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Poulos

    how does ajax affect execution of code

    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
  • David Dorward

    #2
    Re: how does ajax affect execution of code

    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
    David Dorward's mostly neglected blog

    David Dorward's mostly neglected blog

    Comment

    • Gregor Kofler

      #3
      Re: how does ajax affect execution of code

      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

      Comment

      • Andrew Poulos

        #4
        Re: how does ajax affect execution of code

        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 onreadystatecha nge has a
        readyState equal to 4? Does execution stop whenever its up to and run
        the code under the onreadystatecha nge function?

        Andrew Poulos

        Comment

        • Henry

          #5
          Re: how does ajax affect execution of code

          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 onreadystatecha nge
          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 onreadystatecha nge function?
          Execution stops and code runs? That seems a bit contradictory. Are you
          asking whether events interrupt already executing code? They don't.

          Comment

          • Gregor Kofler

            #6
            Re: how does ajax affect execution of code

            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 onreadystatecha nge has a
            readyState equal to 4? Does execution stop whenever its up to and run
            the code under the onreadystatecha nge 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

            Comment

            • Andrew Poulos

              #7
              Re: how does ajax affect execution of code

              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 onreadystatecha nge has a
              >readyState equal to 4? Does execution stop whenever its up to and run
              >the code under the onreadystatecha nge 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

              Comment

              • Gregor Kofler

                #8
                Re: how does ajax affect execution of code

                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.onreadystat echange = function() {
                if (xhr.readystate === 4) {
                foo(xhr.respons eText);
                }
                }
                var foo = function(respon seText) {
                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

                Comment

                Working...