I have the following code. I am able to call the function “titlename” and execute the StatList function to read the CSV file. However I am not able to return any value.From browsing online I found that this question might be related to Ajax, but I donot know Ajax. I want to return the “titles” variable in the following example.
function titlename(title_number)
{
jQuery.get('TitleName.csv', function StatList(data)
{
var titles = new Array();
var title_array = jQuery.csv(undefined, undefined, '\r\n')(data);
for (var i=0; i<title_number.length; i++){
for (var j = 1; j< title_array.length; j++)
{
var tmp_compare = title_array[j].slice(0,title_array[j].length-1);
if(tmp_compare.toString() == title_number[i]){
var title = title_array[j][(title_array[j].length-1)];
//console.log(title);
titles.push(title);
break;
}
}
}
return titles;
});
}
Since AJAX is asynchronous you cannot simply return the value from a call to
titlename. What you could do to use the information once it arrives is call another function and pass in the data you wanted to fetch as an argument. You can either process the data on the spot or process it in the function that you run. For demonstrative purposes, I’ll leave your code where it was.Here’s an example of a potential next step: