Hi everyone, I’m facing some few problems with my javascript code, as to my understanding probably related to asynchronous functions.
I have some large txt files that I want to import in my javascript code, everything is under the same domain.
For instance, when I open /index.php I want Javascript to load text from several text files located in: /log/ip_number/log.txt
At the moment I’m using something like this:
for (var i = 0; i < numberOfVPS; i++) { $.get("/log/"+ip_list[i]+"/log.txt", function(contents){ //does my things with contents array.push(myOutput) }); }
at the end of the code, I should get an array ordered by the order of IPs in the ip_list array… but that’s not what I get and I can’t really understand why…
For example, I should get:
array = [1, 2, 3, 4, 5]
where 1, 2, 3, 4 and 5 are related to ip_list[0], ip_list[1], ip_list[2], ip_list[3], ip_list[4],
but I get something like:
array = [2, 5, 3, 1, 4], and everytime I reload the page there is a different order…
I think that’s due to the fact that the javascript code didn’t manage to load the text files and thus give me some unordered output, but that’s just my opinion, the opinion of a newbie “programmer”…
Please help me with that!
Jerry