How can I hold information in an array in javascript that I've retrieved from a text file for later use? I'm using it to place some HTML but also to react to the user. As of now I can place the HTML just fine using inline function calls, but I want that data to use later...
function get_words() {
var words = new Array();
var sylls = new Array();
var csv_file = new Array(); // for word arrays
$.get('terms.csv', function(data){
csv_file = data.split('\n');
// csv file is now in an array, split into seperate word array and syllable array
for (var i = 0; i < csv_file.length; i++) {
var both = csv_file[i].split(','); // split at the comma
words[i] = both[0]; // populate word array
sylls[i] = both[1]; // populate syllable array
put_word(words[i], sylls[i]);
};
check_resize();
});
}
function put_word(word, sylls) {
console.log(word);
// place the words into 'words' div
var divID = document.getElementById("words"); // grab 'words' div
divID.innerHTML += "<span>" + word + "</span>" + "<sup>" + sylls + "</sup> ";
}
That's the code I have. I'd like it if words[] and sylls[] were accessible outside that get function.
EDIT: Let me be more clear (oops). It doesn't matter where I declare my arrays. The reason I know this is because I can put them at the top of my script (outside of a function) and at the end of get_words() try console.log(words) and it will be an empty array.
var words = new Array();
var sylls = new Array();
var csv_file = new Array(); // for word arrays
$(document).ready(function(){
get_words();
});
function get_words() {
$.get('terms.csv', function(data){
csv_file = data.split('\n');
// csv file is now in an array, split into seperate word array and syllable array
for (var i = 0; i < csv_file.length; i++) {
var both = csv_file[i].split(','); // split at the comma
words[i] = both[0]; // populate word array
sylls[i] = both[1]; // populate syllable array
//put_word(words[i], sylls[i]);
};
check_resize();
});
console.log(words);
}
EDIT: Can someone tell me where to put a callback??
function get_words() {
$.get('terms.csv', function(data){
csv_file = data.split('\n');
// csv file is now in an array, split into seperate word array and syllable array
for (var i = 0; i < csv_file.length; i++) {
var both = csv_file[i].split(','); // split at the comma
words[i] = both[0]; // populate word array
sylls[i] = both[1]; // populate syllable array
//put_word(words[i], sylls[i]);
};
});
}
So... if I want to wait until after this file has been put into the array, then call another function, how do I do that?