Javascript - why do I sometimes fail to read file content with GDownloadUrl?
I try to read some file with google's GDownloadUrl and it works only from time to time.
- failure means fileRows == "blah blah"
- su开发者_JAVA技巧ccess means fileRows == (real file content)
I've noticed, however, that when I cease (with Firebug) the execution on line 3 for a couple of seconds, it succeeds more often. Maybe it is some kind of threading bug, then? Do You guys have any tip or idea?
1 var fileContent = "blah blah";
2 availabilityFile = "input/available/" + date + ".csv";
3 GDownloadUrl(availabilityFile, function(fileData) {
4 fileContent = fileData;
5 });
6 fileRows = fileContent.split("\n");
GDownloadUrl is an asynchronous operation. So, Line 6 is executed immediately, without waiting for GDownloadUrl to finish.
Use the onload function to do the things that can only be done after the download is complete.
Thanks for the explanation. So it seems, that such a code should word as expected.
1 var fileContent = "blah blah";
2 availabilityFile = "input/available/" + date + ".csv";
3 GDownloadUrl(availabilityFile, function(fileData) {
4 fileRows = fileData.split("\n");
5 });
精彩评论