var testFile = $("#selection").val(),
testData = getTestData(testFile);
// not working during the first time I run the function, empty
alert(testData);
function getTestData(testF) {
$.getJSON("test.php", {
fileTest: testF
}, function (data) {
$.each(data, function (index, value) {
if (value != "") {} else {
testArray[index] = value;
}
});
});
// working I see the values
alert(testArray);
// not working the first time running this function
return testArray;
}
var testFile = $(#selection).val(), testData = getTestData(testFile); // not working during the first time
Share
AJAX is asynchronous .. So your function must have returned before the values were stored into it..
Better to store it in a higher scoped variable and use it once the request is done..