3 Replies Latest reply on May 16, 2009 6:13 PM by Ned Murphy

    Add a time delay before pushing to an array?

    mikeyjray Level 1

      I have a function, loadXML, that loads an xml file with a list of locations to OTHER xml files.  Within this function I load each individual xml file and run a new function, parseXML, to pass each of the variables I desire from the individual xml files into an array.

       

      This all works fine and dandy on my computer, but when I run it online it breaks down.

       

      What I believe is going on is that the initial xml file loads fine, but because the referenced xml files are on the web and are varying sizes, they stop loading sequentially and the data no longer corresponds to the xml file referenced in referenceListA array.  (ie. all the data from each referenced file, item1, item2, and item3 will stay together because they are called at the same time, but will become mixed up from the referenced xml file).

       

      This normally would not be a big deal, but because I want to also create a link BACK to the referenced xml file, sometimes the link will not lead back to right location (it will lead back to another xml file in the list).

       

      Does anyone have any ideas?  I was thinking about adding a time delay somehow in the loadXML function to give time to load the newxml file, but that would still be problematic...

       

      How can I make sure that the referenceListA[i] mathes up to its own information?

       

      ---

       

      referenceListA = [];

       

      xmlData = new XML();
      xmlData.ignoreWhite = true;
      xmlData.onLoad = loadXML;
      xmlData.load("list.xml");
      function loadXML(loaded) {
          if (loaded) {
              var root = this.firstChild; // The root node
              for(var i = root.firstChild; i !=null; i = i.nextSibling){

                  referenceListA.push(i.attributes.location);
                  newxml = new XML();
                  newxml.ignoreWhite = true;
                  newxml.onLoad = parseXML;
                  newxml.load(i.attributes.location+"details.xml");;
              }
          } else {
              trace("file not loaded!");
          }
      }

       

      item1A= [];
      item2A = [];
      item3A = [];
      function parseXML(){
          item1A.push(this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue);
          item2A.push(this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue);
          item3A.push(this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue);
      }

       

      trace(referenceListA);
      trace(item1A);
      trace(item2A);
      trace(item3A);

        • 1. Re: Add a time delay before pushing to an array?
          Ned Murphy Adobe Community Professional & MVP

          Normally, if you want to have sequencial loading, you manage it such the one loading completion triggers the next.

           

          So rather than having the for loop in the loadXML function, end it with capturing the data in the array and kick off a new function (A) that utilizes a counter variable to load each file using the array, one at a time--function A only loads one file.  The onLoad function (B) defined in function A would parse the file you load and then call function A again to load the next file.  You would test in function B if all files have been loaded using the length of the array that stored the filenames.

          1 person found this helpful
          • 2. Re: Add a time delay before pushing to an array?
            mikeyjray Level 1

            Hi Ned,

             

            OK...I wish I knew more flash!

             

            I left the first for loop to store the xml references, and now I'm trying to run a second for loop to parse the xml...I don't know if this is what you were thinking, so any help would be great!

             

            ---


            referenceListA = [];

             

            xmlData = new XML();
            xmlData.ignoreWhite = true;
            xmlData.onLoad = loadXML;
            xmlData.load("list.xml");
            function loadXML(loaded) {
                if (loaded) {
                    var root = this.firstChild; // The root node
                    for(var i = root.firstChild; i !=null; i = i.nextSibling){
                        referenceListA.push(i.attributes.location);

                    }

                functionB();

                functionA();

                } else {
                    trace("file not loaded!");
                }

            }

             

            locationA = [];

            item1A= [];
            item2A = [];
            item3A = [];

             

            var num:Number = 0;

             

            function functionA(){

                    num = num++;

                    functionB();

            }

             

            function functionB(){

                    referenceListA[num];

                    newxml = new XML();
                     newxml.ignoreWhite = true;
                     newxml.onLoad = parseXML;
                     newxml.load(i.attributes.location+"details.xml");
            }


            function parseXML(){

                locationA.push(this);
                item1A.push(this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue);
                item2A.push(this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue);
                item3A.push(this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue);

                functionA();
            }

             

            trace(referenceListA);
            trace(item1A);
            trace(item2A);
            trace(item3A);

            • 3. Re: Add a time delay before pushing to an array?
              Ned Murphy Adobe Community Professional & MVP

              I can't be sure what you have declared where and what some of the things are in your code, but what I was describing is more like what I show below--this is a quick edit, so just try to get the idea from it...

               

              referenceListA = [];

              locationA = [];

              item1A= [];
              item2A = [];
              item3A = [];

               

               

              var xmlData = new XML();
              xmlData.ignoreWhite = true;
              xmlData.onLoad = loadXML;
              xmlData.load("list.xml");
              function loadXML(loaded) {
                  if (loaded) {
                      var root = this.firstChild; // The root node
                      for(var i = root.firstChild; i !=null; i = i.nextSibling){
                          referenceListA.push(i.attributes.location);

                      }

                  functionA(); // load the first file

                  } else {
                      trace("file not loaded!");
                  }

              }

               

              locationA = [];

              item1A= [];
              item2A = [];
              item3A = [];

               

              var num:Number = 0;

               

              function functionA(){  // load the current file identified by index value num

                       newxml = new XML();
                       newxml.ignoreWhite = true;
                       newxml.onLoad = parseXML;
                       newxml.load(referenceListA[num]+"details.xml"); 
              }


              function parseXML(){  // what I called function B

                  locationA.push(this);
                  item1A.push(this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue);
                  item2A.push(this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue);
                  item3A.push(this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue);

                  num += 1;

                   if(num < referenceListA.length){

                        functionA();    // load the next file

                   }
              }