-1

first function gets json from a url and pushes it into array

var resultsArr = new Array();

// getResults()
// JSON response function -> takes uuid returned from search query
// and pings reponse url for any/all JSON objects of returned data
function getResults(req_uuid) {
    console.log("url fetched");
    $.getJSON( $SCRIPT_ROOT + "/respond?id=" + req_uuid, function(data) {
        console.log(data);
        resultsArr.push(data);
    });
}

I have another function that runs on a click of a button that will pass uuid into getResults() at a given interval. This is all working. I'm even converting the entire array to a string and appending it into a div just before the call to the function in question. (this is also working). I'm logging the number of elements in the array before the call;

console.log( resultsArr.length + " items in array")

this logs = 4 items in array

then the function is called;

function showResults(array) {
    var index = array.length;
    console.log( index + " items in array");

}

this returns undefined items in array. I am severely baffled.

frankV
  • 5,353
  • 8
  • 33
  • 46
  • What's the scope of your `resultsArr`? Make sure it is scoped globally, i.e. not defined within a function, if you are going to make such global references to it. – Candide Jul 10 '13 at 03:36
  • 4
    how is `showResults` called? – Arun P Johny Jul 10 '13 at 03:39
  • See this: http://stackoverflow.com/questions/17460556/undefined-return-value-from-the-function-call-javascritpt/17460802#17460802 – slebetman Jul 10 '13 at 04:17
  • 1
    As for how to do this in a loop (from you comment on an answer) see this: http://stackoverflow.com/questions/4631774/coordinating-parallel-execution-in-node-js/4631909#4631909 – slebetman Jul 10 '13 at 04:20
  • Also this for handling async loops: http://stackoverflow.com/questions/13250746/process-chain-of-functions-without-ui-block/13252018#13252018 – slebetman Jul 10 '13 at 04:20

1 Answers1

1

You want to call showResults() after you've received the new data and pushed it to the array within the ajax callback.

var resultsArr = new Array();

// getResults()
// JSON response function -> takes uuid returned from search query
// and pings reponse url for any/all JSON objects of returned data
function getResults(req_uuid) {
    console.log("url fetched");
    $.getJSON( $SCRIPT_ROOT + "/respond?id=" + req_uuid, function(data) {
        console.log(data);
        resultsArr.push(data);
        showResults(resultsArr);// <--- here
    });
}
Dennis Martinez
  • 6,344
  • 11
  • 50
  • 67
  • but that will redisplay each previous element since `showResults` is stepping through `resultsArr` with a for loop – frankV Jul 10 '13 at 03:48
  • 1
    Pardon my ignorance, but I don't see a loop inside of the `showResults()` function. I could be possibly missing something. Can you post more code if there is a loop involved? – Dennis Martinez Jul 10 '13 at 03:50
  • you're right I didn't post it. I just pasted up to the relevant section for the sake of brevity. Thanks for the help, though. – frankV Jul 10 '13 at 03:59