Convert array to string after "for loop" is finished

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

    Convert array to string after "for loop" is finished

    I am having problems getting values out of an array. The array is set
    as a global array and values are pushed into it as they are read from
    a JSON file using a "for loop". When the "for loop" is finished I want
    to convert the array into a string which can be used by another
    function. My attempt to do this is not working. The script looks like
    this:

    heights=[];
    function getElevationInt er(latv,lngv) {
    var script = document.create Element('script ');
    document.body.a ppendChild(scri pt);
    script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
    '&lng=' + lngv + '&callback=load JSON';
    }

    function loadJSON(result ) {
    heights.push(re sult.srtm3);
    }

    function getProfile() {
    var dLat = lat[2]-lat[1];
    var dLng = lng[2]-lng[1];
    for (var i=0; i<d; i+=0.5) {
    var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
    var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
    getElevationInt er(latPoints,ln gPoints);
    }
    //alert(i)
    if (i>d) {
    alert(heights[2])
    heightString=he ights.toString( )
    alert(heightStr ing)
    }
    }

    Strangely, if I remove the comment // from the alert(i) the script
    sometimes works but with the alert(i) commented out it never works.
    The alert(heights[2] is there as a test, it is usually undefined
    except sometimes when the alert(i) is uncommented when it returns the
    correct value.

    I would be grateful for any assistance.
  • Doug Gunnoe

    #2
    Re: Convert array to string after &quot;for loop&quot; is finished

    On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
    I am having problems getting values out of an array. The array is set
    as a global array and values are pushed into it as they are read from
    a JSON file using a "for loop". When the "for loop" is finished I want
    to convert the array into a string which can be used by another
    function. My attempt to do this is not working. The script looks like
    this:
    >
    heights=[];
    function getElevationInt er(latv,lngv) {
    var script = document.create Element('script ');
    document.body.a ppendChild(scri pt);
    script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
    '&lng=' + lngv + '&callback=load JSON';
    >
    }
    >
    function loadJSON(result ) {
    heights.push(re sult.srtm3);
    >
    }
    >
    function getProfile() {
    var dLat = lat[2]-lat[1];
    var dLng = lng[2]-lng[1];
    for (var i=0; i<d; i+=0.5) {
    var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
    var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
    getElevationInt er(latPoints,ln gPoints);
    }
    //alert(i)
    if (i>d) {
    alert(heights[2])
    heightString=he ights.toString( )
    alert(heightStr ing)
    }
    >
    }
    >
    Strangely, if I remove the comment // from the alert(i) the script
    sometimes works but with the alert(i) commented out it never works.
    The alert(heights[2] is there as a test, it is usually undefined
    except sometimes when the alert(i) is uncommented when it returns the
    correct value.
    >
    I would be grateful for any assistance.
    Do you have a local variable in any function in your script 'http://
    ws.geonames.org/srtm3JSON' named heights?

    If so, try using the var keyword for your global above. 'var
    heights=[]' instead of just 'heights=[]'.

    The following paragraph was taken from here
    The var statement declares function-scoped or globally-scoped variables, optionally initializing each to a value.


    Using var outside a function is optional; assigning a value to an
    undeclared variable implicitly declares it as a global variable.
    However, it is recommended to always use var, and it is necessary
    within functions in the following situations:

    * If a variable in a scope containing the function (including the
    global scope) has the same name.
    * If recursive or multiple functions use variables with the same
    name and intend those variables to be local.

    Failure to declare the variable in these cases will very likely lead
    to unexpected results.

    Comment

    • Doug Gunnoe

      #3
      Re: Convert array to string after &quot;for loop&quot; is finished

      On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
      I am having problems getting values out of an array. The array is set
      as a global array and values are pushed into it as they are read from
      a JSON file using a "for loop". When the "for loop" is finished I want
      to convert the array into a string which can be used by another
      function. My attempt to do this is not working. The script looks like
      this:
      >
      heights=[];
      function getElevationInt er(latv,lngv) {
      var script = document.create Element('script ');
      document.body.a ppendChild(scri pt);
      script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
      '&lng=' + lngv + '&callback=load JSON';
      >
      }
      >
      function loadJSON(result ) {
      heights.push(re sult.srtm3);
      >
      }
      >
      function getProfile() {
      var dLat = lat[2]-lat[1];
      var dLng = lng[2]-lng[1];
      for (var i=0; i<d; i+=0.5) {
      var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
      var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
      getElevationInt er(latPoints,ln gPoints);
      }
      //alert(i)
      if (i>d) {
      alert(heights[2])
      heightString=he ights.toString( )
      alert(heightStr ing)
      }
      >
      }
      >
      Strangely, if I remove the comment // from the alert(i) the script
      sometimes works but with the alert(i) commented out it never works.
      The alert(heights[2] is there as a test, it is usually undefined
      except sometimes when the alert(i) is uncommented when it returns the
      correct value.
      >
      I would be grateful for any assistance.
      Possibly a scope problem with 'heights=[];' ?

      Comment

      • RobG

        #4
        Re: Convert array to string after &quot;for loop&quot; is finished

        On Jun 23, 7:20 am, Steve <stephen.jo...@ googlemail.comw rote:
        I am having problems getting values out of an array. The array is set
        as a global array and values are pushed into it as they are read from
        a JSON file using a "for loop". When the "for loop" is finished I want
        to convert the array into a string which can be used by another
        function. My attempt to do this is not working. The script looks like
        this:
        >
        heights=[];
        You should always declare variables with var, although that may not be
        an issue in this case.

        var heights=[];

        function getElevationInt er(latv,lngv) {
        var script = document.create Element('script ');
        document.body.a ppendChild(scri pt);
        script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
        '&lng=' + lngv + '&callback=load JSON';
        >
        }
        >
        function loadJSON(result ) {
        heights.push(re sult.srtm3);
        >
        }
        >
        function getProfile() {
        var dLat = lat[2]-lat[1];
        What is the result of that? Is it a number? See below.

        var dLng = lng[2]-lng[1];
        for (var i=0; i<d; i+=0.5) {
        var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
        var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
        There seems to be a misplaced bracket in the above, -----^

        Why have you used parseFloat? What are the values in the lat and lng
        arrays? If they are numbers, there is no need for parseFloat. If
        they are strings, you need to use parseFloat on the individual values,
        e.g.

        var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);

        The use of multiplication and division will automatcially convert
        dLat, i and d to numbers if they are strings (provided they are
        strings that can be converted to numbers, like 23.45).

        getElevationInt er(latPoints,ln gPoints);
        Here you load the data, but do you wait for the element to become
        available?

        }
        //alert(i)
        if (i>d) {
        alert(heights[2])
        heightString=he ights.toString( )
        alert(heightStr ing)
        }
        >
        }
        >
        Strangely, if I remove the comment // from the alert(i) the script
        sometimes works but with the alert(i) commented out it never works.
        The alert(heights[2] is there as a test, it is usually undefined
        except sometimes when the alert(i) is uncommented when it returns the
        correct value.
        That seems like a timing issue. Are you wating for the script element
        to exist before trying to use the content? Try puting the statements
        after the alter into a separate function and call it after a delay of
        say 1000ms or so (or maybe 2000ms to be sure).


        --
        Rob

        Comment

        • Steve

          #5
          Re: Convert array to string after &quot;for loop&quot; is finished

          On Jun 23, 3:54 am, Doug Gunnoe <douggun...@gma il.comwrote:
          On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
          >
          >
          >
          I am having problems getting values out of an array. The array is set
          as a global array
          >
          Possibly a scope problem with 'heights=[];' ?
          The scope of heights=[] is Global.

          Steve.

          Comment

          • Steve

            #6
            Re: Convert array to string after &quot;for loop&quot; is finished

            Thanks for the reply but.........

            On Jun 23, 5:08 am, RobG <rg...@iinet.ne t.auwrote:
            >
            var dLng = lng[2]-lng[1];
            for (var i=0; i<d; i+=0.5) {
            var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
            var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
            >
            There seems to be a misplaced bracket in the above, -----^
            You're right, I'll correct this when I get home tonight.
            >
            Why have you used parseFloat? What are the values in the lat and lng
            arrays? If they are numbers, there is no need for parseFloat. If
            they are strings, you need to use parseFloat on the individual values,
            e.g.
            >
            var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
            >
            The use of multiplication and division will automatcially convert
            dLat, i and d to numbers if they are strings (provided they are
            strings that can be converted to numbers, like 23.45).
            I used parsefloat because of the problems I was having. I am aware
            that is not necessary but it shouldn't matter or cause a problem
            should it?
            >
            getElevationInt er(latPoints,ln gPoints);
            >
            Here you load the data, but do you wait for the element to become
            available?
            >
            }
            //alert(i)
            if (i>d) {
            alert(heights[2])
            heightString=he ights.toString( )
            alert(heightStr ing)
            }
            }
            >
            Yes, because if I put an alert(heights) in the loadJSON function like
            below the result values are displaed in the alert.

            function loadJSON(result ) {
            heights.push(re sult.srtm3);
            alert(heights)
            }

            Strangely, if I remove the comment // from the alert(i) the script
            sometimes works but with the alert(i) commented out it never works.
            The alert(heights[2] is there as a test, it is usually undefined
            except sometimes when the alert(i) is uncommented when it returns the
            correct value.
            >
            That seems like a timing issue. Are you wating for the script element
            to exist before trying to use the content? Try puting the statements
            after the alter into a separate function and call it after a delay of
            say 1000ms or so (or maybe 2000ms to be sure).
            Maybe, but why should a delay be necessary as I know that the values
            are in the array, see above comments on loadJSON.


            Comment

            • RobG

              #7
              Re: Convert array to string after &quot;for loop&quot; is finished

              On Jun 23, 4:58 pm, Steve <stephen.jo...@ googlemail.comw rote:
              Thanks for the reply but.........
              >
              On Jun 23, 5:08 am, RobG <rg...@iinet.ne t.auwrote:
              >
              >
              >
              var dLng = lng[2]-lng[1];
              for (var i=0; i<d; i+=0.5) {
              var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
              var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
              >
              There seems to be a misplaced bracket in the above, -----^
              >
              You're right, I'll correct this when I get home tonight.
              >
              >
              >
              Why have you used parseFloat? What are the values in the lat and lng
              arrays? If they are numbers, there is no need for parseFloat. If
              they are strings, you need to use parseFloat on the individual values,
              e.g.
              >
              var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
              >
              The use of multiplication and division will automatcially convert
              dLat, i and d to numbers if they are strings (provided they are
              strings that can be converted to numbers, like 23.45).
              >
              I used parsefloat because of the problems I was having. I am aware
              that is not necessary but it shouldn't matter or cause a problem
              should it?
              No, I was just seeing if you expect it to do anything that it might
              not be doing.

              getElevationInt er(latPoints,ln gPoints);
              >
              Here you load the data, but do you wait for the element to become
              available?
              >
              }
              //alert(i)
              if (i>d) {
              alert(heights[2])
              heightString=he ights.toString( )
              alert(heightStr ing)
              }
              }
              >
              Yes, because if I put an alert(heights) in the loadJSON function like
              below the result values are displaed in the alert.
              >
              function loadJSON(result ) {
              heights.push(re sult.srtm3);
              alert(heights)
              >
              }
              The code you posted doesn't call loadJSON, it is included in the URI
              for the script element's src attribute. Where do you call it from?
              Strangely, if I remove the comment // from the alert(i) the script
              sometimes works but with the alert(i) commented out it never works.
              The alert(heights[2] is there as a test, it is usually undefined
              except sometimes when the alert(i) is uncommented when it returns the
              correct value.
              >
              That seems like a timing issue. Are you wating for the script element
              to exist before trying to use the content? Try puting the statements
              after the alter into a separate function and call it after a delay of
              say 1000ms or so (or maybe 2000ms to be sure).
              >
              Maybe, but why should a delay be necessary as I know that the values
              are in the array, see above comments on loadJSON.
              Did you try it?

              The reason I think it might be an issue is that after you assign a
              value to the DOM element's src property, the function moves on. I
              don't think there is anything that says it should wait until the
              script file has actually finished loading and code executed before
              moving to the next step in the function.


              --
              Rob

              Comment

              • Steve

                #8
                Re: Convert array to string after &quot;for loop&quot; is finished

                On Jun 23, 9:11 am, RobG <rg...@iinet.ne t.auwrote:
                On Jun 23, 4:58 pm, Steve <stephen.jo...@ googlemail.comw rote:
                >
                >
                >
                Thanks for the reply but.........
                >
                On Jun 23, 5:08 am, RobG <rg...@iinet.ne t.auwrote:
                >
                var dLng = lng[2]-lng[1];
                for (var i=0; i<d; i+=0.5) {
                var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
                var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
                >
                There seems to be a misplaced bracket in the above, -----^
                >
                You're right, I'll correct this when I get home tonight.
                >
                Why have you used parseFloat? What are the values in the lat and lng
                arrays? If they are numbers, there is no need for parseFloat. If
                they are strings, you need to use parseFloat on the individual values,
                e.g.
                >
                var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
                >
                The use of multiplication and division will automatcially convert
                dLat, i and d to numbers if they are strings (provided they are
                strings that can be converted to numbers, like 23.45).
                >
                I used parsefloat because of the problems I was having. I am aware
                that is not necessary but it shouldn't matter or cause a problem
                should it?
                >
                No, I was just seeing if you expect it to do anything that it might
                not be doing.
                >
                >
                >
                getElevationInt er(latPoints,ln gPoints);
                >
                Here you load the data, but do you wait for the element to become
                available?
                >
                }
                //alert(i)
                if (i>d) {
                alert(heights[2])
                heightString=he ights.toString( )
                alert(heightStr ing)
                }
                }
                >
                Yes, because if I put an alert(heights) in the loadJSON function like
                below the result values are displaed in the alert.
                >
                function loadJSON(result ) {
                heights.push(re sult.srtm3);
                alert(heights)
                >
                }
                >
                The code you posted doesn't call loadJSON, it is included in the URI
                for the script element's src attribute. Where do you call it from?
                >
                Strangely, if I remove the comment // from the alert(i) the script
                sometimes works but with the alert(i) commented out it never works.
                The alert(heights[2] is there as a test, it is usually undefined
                except sometimes when the alert(i) is uncommented when it returns the
                correct value.
                >
                That seems like a timing issue. Are you wating for the script element
                to exist before trying to use the content? Try puting the statements
                after the alter into a separate function and call it after a delay of
                say 1000ms or so (or maybe 2000ms to be sure).
                >
                Maybe, but why should a delay be necessary as I know that the values
                are in the array, see above comments on loadJSON.
                >
                Did you try it?
                >
                The reason I think it might be an issue is that after you assign a
                value to the DOM element's src property, the function moves on. I
                don't think there is anything that says it should wait until the
                script file has actually finished loading and code executed before
                moving to the next step in the function.
                >
                --
                Rob
                Hi Rob,

                I will try it this evening and let you know what happens.

                Thanks for your help.

                Steve.

                Comment

                • Doug Gunnoe

                  #9
                  Re: Convert array to string after &quot;for loop&quot; is finished

                  On Jun 23, 1:45 am, Steve <stephen.jo...@ googlemail.comw rote:
                  On Jun 23, 3:54 am, Doug Gunnoe <douggun...@gma il.comwrote:
                  >
                  On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
                  >
                  I am having problems getting values out of an array. The array is set
                  as a global array
                  >
                  Possibly a scope problem with 'heights=[];' ?
                  >
                  The scope of heights=[] is Global.
                  >
                  Steve.
                  Yes I know.

                  My first inclination was that there could have been some issues
                  regarding this.

                  from http://developer.mozilla.org/en/docs...Statements:var
                  --
                  Using var outside a function is optional; assigning a value to an
                  undeclared variable implicitly declares it as a global variable.
                  However, it is recommended to always use var, and it is necessary
                  within functions in the following situations:

                  * If a variable in a scope containing the function (including the
                  global scope) has the same name.
                  * If recursive or multiple functions use variables with the same
                  name and intend those variables to be local.

                  Failure to declare the variable in these cases will very likely lead
                  to unexpected results.
                  --

                  But, after testing the idea out for a little while I decided that this
                  probably wasn't the problem.

                  I am intrigued by RobG's thought that this could be a timing problem.
                  And I too would like to know at what point you are calling functions
                  in the the script you insert into the DOM tree.

                  Good luck with this. Sorry I wasn't any help.

                  Comment

                  • Laser Lips

                    #10
                    Re: Convert array to string after &quot;for loop&quot; is finished

                    I remember having this problem when trying to use google maps. I'm
                    raking my brains to remeber what the soloution was

                    Comment

                    • Steve

                      #11
                      Re: Convert array to string after &quot;for loop&quot; is finished

                      On Jun 23, 9:11 am, RobG <rg...@iinet.ne t.auwrote:
                      On Jun 23, 4:58 pm, Steve <stephen.jo...@ googlemail.comw rote:
                      >
                      >
                      >
                      Thanks for the reply but.........
                      >
                      On Jun 23, 5:08 am, RobG <rg...@iinet.ne t.auwrote:
                      >
                      var dLng = lng[2]-lng[1];
                      for (var i=0; i<d; i+=0.5) {
                      var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
                      var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
                      >
                      There seems to be a misplaced bracket in the above, -----^
                      >
                      You're right, I'll correct this when I get home tonight.
                      >
                      Why have you used parseFloat? What are the values in the lat and lng
                      arrays? If they are numbers, there is no need for parseFloat. If
                      they are strings, you need to use parseFloat on the individual values,
                      e.g.
                      >
                      var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
                      >
                      The use of multiplication and division will automatcially convert
                      dLat, i and d to numbers if they are strings (provided they are
                      strings that can be converted to numbers, like 23.45).
                      >
                      I used parsefloat because of the problems I was having. I am aware
                      that is not necessary but it shouldn't matter or cause a problem
                      should it?
                      >
                      No, I was just seeing if you expect it to do anything that it might
                      not be doing.
                      >
                      >
                      >
                      getElevationInt er(latPoints,ln gPoints);
                      >
                      Here you load the data, but do you wait for the element to become
                      available?
                      >
                      }
                      //alert(i)
                      if (i>d) {
                      alert(heights[2])
                      heightString=he ights.toString( )
                      alert(heightStr ing)
                      }
                      }
                      >
                      Yes, because if I put an alert(heights) in the loadJSON function like
                      below the result values are displaed in the alert.
                      >
                      function loadJSON(result ) {
                      heights.push(re sult.srtm3);
                      alert(heights)
                      >
                      }
                      >
                      The code you posted doesn't call loadJSON, it is included in the URI
                      for the script element's src attribute. Where do you call it from?
                      >
                      Strangely, if I remove the comment // from the alert(i) the script
                      sometimes works but with the alert(i) commented out it never works.
                      The alert(heights[2] is there as a test, it is usually undefined
                      except sometimes when the alert(i) is uncommented when it returns the
                      correct value.
                      >
                      That seems like a timing issue. Are you wating for the script element
                      to exist before trying to use the content? Try puting the statements
                      after the alter into a separate function and call it after a delay of
                      say 1000ms or so (or maybe 2000ms to be sure).
                      >
                      Maybe, but why should a delay be necessary as I know that the values
                      are in the array, see above comments on loadJSON.
                      >
                      Did you try it?
                      >
                      The reason I think it might be an issue is that after you assign a
                      value to the DOM element's src property, the function moves on. I
                      don't think there is anything that says it should wait until the
                      script file has actually finished loading and code executed before
                      moving to the next step in the function.
                      Brilliant! That solved the problem. Just when you start to think that
                      you know JavaScript something like this happens ;-)


                      Thanks to all who replied and just to show how it was done here is the
                      altered script:-

                      var heights=[];
                      function getElevationInt er(latv,lngv) {
                      var script = document.create Element('script ');
                      document.body.a ppendChild(scri pt);
                      script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
                      '&lng=' + lngv + '&callback=load JSON';
                      }

                      function loadJSON(result ) {
                      heights.push(re sult.srtm3)
                      }

                      function getProfile() {
                      var dLat = lat[2]-lat[1];
                      var dLng = lng[2]-lng[1];
                      for (var i=0; i<d; i+=0.5) {
                      var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
                      var lngPoints = parseFloat((lng[1] + dLng*i/d)/10);
                      getElevationInt er(latPoints,ln gPoints);
                      }
                      setTimeout(heig htToString,1000 )
                      }

                      function heightToString( ) {
                      alert(heights[2]);
                      heightString=he ights.toString( );
                      alert(heightStr ing);
                      }


                      Thanks again,

                      Steve.

                      Comment

                      • Dr J R Stockton

                        #12
                        Re: Convert array to string after &quot;for loop&quot; is finished

                        In comp.lang.javas cript message <4f99b1b0-1853-4dd5-b1f4-b93a3f998a75@x3
                        5g2000hsb.googl egroups.com>, Sun, 22 Jun 2008 14:20:23, Steve
                        <stephen.joung@ googlemail.comp osted:
                        >I am having problems getting values out of an array. The array is set
                        >as a global array and values are pushed into it as they are read from
                        >a JSON file using a "for loop". When the "for loop" is finished I want
                        >to convert the array into a string which can be used by another
                        >function.

                        When something peculiar is seen, state which browser you are using; it
                        might matter.

                        The FOR loop is counting in half-integers. If d is half-integer, it
                        leaves i==d, otherwise i>d. That might be relevant.

                        The alert will cause a delay, which might matter. Consider trying,
                        instead, alert(heights).

                        ISTM that your FOR loop invokes geonames a number of times in rapid
                        sequence; there may be no guarantee that the responses come in the same
                        sequence, which might matter.

                        Instead of using a FOR loop, you could make the first geonames call and
                        use the callback function to make, or schedule, the next call, if not
                        finished.

                        --
                        (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
                        Web <URL:http://www.merlyn.demo n.co.uk/- FAQqish topics, acronyms & links;
                        Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
                        No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                        Comment

                        • Doug Gunnoe

                          #13
                          Re: Convert array to string after &quot;for loop&quot; is finished

                          On Jun 23, 11:29 am, Steve <stephen.jo...@ googlemail.comw rote:
                              setTimeout(heig htToString,1000 )

                          Good call Rob.

                          Comment

                          • Steve

                            #14
                            Re: Convert array to string after &quot;for loop&quot; is finished

                            On Jun 23, 1:02 pm, Dr J R Stockton <j...@merlyn.de mon.co.ukwrote:
                            Instead of using a FOR loop, you could make the first geonames call and
                            use the callback function to make, or schedule, the next call, if not
                            finished.
                            >
                            As you will have seen the problem has been solved but I am intrigued
                            by your suggestion to use call and callback functions as I am not
                            aware of how these functions work. Could you give an example please?

                            Regards, Steve.

                            Comment

                            • Dr J R Stockton

                              #15
                              Re: Convert array to string after &quot;for loop&quot; is finished

                              In comp.lang.javas cript message <d551d2b3-2a42-4eae-afdd-12ba7adbf445@a7
                              0g2000hsh.googl egroups.com>, Tue, 24 Jun 2008 00:58:03, Steve
                              <stephen.joung@ googlemail.comp osted:
                              >On Jun 23, 1:02 pm, Dr J R Stockton <j...@merlyn.de mon.co.ukwrote:
                              >
                              >Instead of using a FOR loop, you could make the first geonames call and
                              >use the callback function to make, or schedule, the next call, if not
                              >finished.
                              >>
                              >As you will have seen the problem has been solved but I am intrigued
                              >by your suggestion to use call and callback functions as I am not
                              >aware of how these functions work. Could you give an example please?
                              You are already using one, if I have the terminology correct. Indeed,
                              your code includes '&callback=load JSON'; .

                              Make the content of your loop a function; instead of the loop, call it
                              once, with the first datum. When the result appears, loadJSON will be
                              called; at the end of that routine, if there are more data points to be
                              processed, call the function again, otherwise call the terminal
                              processing of heights.

                              Using a timeout within the loop, you are navigating between the Scylla
                              of wasting time and the Charybdis of not always waiting long enough -
                              remember that the response-time of geonames is likely yo vary
                              considerably and unpredictably. But, since it is possible that geonames
                              may not be working, I suggest an overall timeout calling a prompt for
                              choosing between waiting again and abandoning all hope.

                              --
                              (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
                              Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
                              Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
                              Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)

                              Comment

                              Working...