javascript - JS Array values become undefined -


this question has answer here:

i have function pull information xml file located on system. pull values located in file , put them array. once function called values enter array, onnce function ends values go away.

 function getxml(location,day){             $(document).ready(function () {                 $.ajax({                     type:'post',        //just echo                     datatype: "xml", // type of file trying read                     crossdomain:true,                     url: './../currentfiles/'+ location +'.xml', // name of file want parse                     success: function (xmldata){                         if(array[0] == null){                             $(xmldata).find('dgauges').children().each(function(){                                array.push($(this).text());                              });                         }                      }, // name of function call upon success                     error: function(xhr, status, error) {                          console.log(error);                         console.log(status);                     }                 });             });              return array[day];         } 

from researched problem async, not understand entirely is. new jquery if there thing seems out of place that's why.

this plan function

i have xml file formatted like

<dgages><d>26.850</d><d-1>7.70</d-1><d-2>2.00</d-2><d-3>27.90</d-3></dgages> 

i trying pull of values in array can calculations on them.

  1. get xml document
  2. find children of dgage
  3. put each of children array 4 once array filled return associated day.

try making synchronous call instead. ajax calls async default, means code jump next line before waiting result of previous line. can enforce result telling ajax call synchoronously:

function getxml(location, day) {     var array = [];     $.ajax({         type: 'post', //just echo         datatype: "xml", // type of file trying read         crossdomain: true,         async: false, // wait until ajax call completed         url: './../currentfiles/' + location + '.xml', // name of file want parse         success: function(xmldata) {             if (array[0] == null) {                 $(xmldata).find('dgauges').children().each(function() {                     array.push($(this).text());                 });             }         }, // name of function call upon success         error: function(xhr, status, error) {             console.log(error);             console.log(status);         }     });      return array[day]; } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -