0

I read a xml file as an input. I receive "undefined" as my return value while, when I put an alert message above that return bizzarely I get my correct answer ! I checked one similar problem in this topic but it didn't work for me. Seeking for help !

<script>
function FindingChildrenWithFather(Father)
{
 var NodesDetails = [];
  $.get('Flow.xml', function(d){
          $(d).find('User').each(function(){
           if ($(this).attr("SenderId") == Father) 
    {
     NodesDetails.push($(this).attr("Id") );
           }
   });
  });
                //alert();
  return NodesDetails;
 } 
  $(document).ready(function(){ 
   var xx = FindingChildrenWithFather('');
  alert (xx);
 });
</script>
Community
  • 1
  • 1
Ali_R
  • 1
  • 1
  • there is delay to get data. – Sherali Turdiyev Oct 10 '15 at 16:20
  • try to move `return NodesDetails;` into your condition – Sherali Turdiyev Oct 10 '15 at 16:23
  • @SheraliTurdiyev: That wouldn't help. That `return` would be in the nested function, and the outer function will have returned long before it gets called anyway. –  Oct 10 '15 at 16:25
  • 1
    Ali_R: FYI, the `alert()` made it "work" because it forced the function to pause long enough for the AJAX response to return. Use `console.log()` for debugging. It's much better on many levels. Then study the duplicate. Lots of people get hung up on this, but it's quite easy once you understand what's happening. –  Oct 10 '15 at 16:26
  • @squint. sorry my english. what is FYI? – Sherali Turdiyev Oct 10 '15 at 16:27
  • 1
    @SheraliTurdiyev: *"For your information"* Just giving a little extra detail. ;-) –  Oct 10 '15 at 16:28

0 Answers0