0

I am trying to populate a javascript array using getJSON. The array needs to be available to all functions on a given page.

I have no trouble getting the array into the the embedded anonymous function but can't seen to get it into the parent function so it can be returned.

    $bogus = rhash();
    function addresslist(){
    var $addresses = [];

    $.getJSON ('/personal/pdo_getaddresses.php', {bogus:$bogus}
    .done, function ($result){
    $addresses = $result.slice(0);
       console.log ('Length 01 = ' + $addresses.length)
    })
    console.log ('Length 02 = ' + $addresses.length)
    //return $addresses;
 }

The console log shows

Length 01 = 438

and

Length 02 = 0

I guess that in simple terms, I need to understand how to make the function addresslist return the data that is returned asynchronously to the anonymous function.

I've been at this all afternoon. Your advice is sincerely appreciated.

Dave Davis
  • 574
  • 1
  • 4
  • 14
  • ajax request is done asynchronously. so, at the moment of `console.log ('Length 02 = ' + $addresses.length)` this request is not completed and interpreter exists the function `addresslist`. you should call the function which should work with received data within `function ($result)` – Cheery Dec 12 '14 at 23:44
  • I'm pretty sure it logs `Length 02 = 0` first. – Felix Kling Dec 12 '14 at 23:47

0 Answers0