I just want to get data from this text file, parse it a bit, and toss it into a couple of arrays. The asynchronous nature of aJax (which I didn't even know I was using...?) means that the array is still being populated before I try to access it. This seems to create a totally useless situation to me, as I need to access the arrays at different times during the users visit to the site. Any ideas?
var words = new Array();
var sylls = new Array();
var csv_file = new Array(); // for word arrays
$(document).ready(function(){
readWords( addWords );
});
function readWords( process ) {
$.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]);
};
});
process(words, sylls);
}
function addWords(w, ss){
console.log(w);
}
this all eventually returns an empty array.
EDIT--SOLUTION:
I'm not sure why no one suggested this before, but for those of you as frustrated with ajax as I am, here is the easy solution!
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){
//async: false;
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]);
};
})
.done(function() {
// once it's done DO THIS STUFF
});
}