javascript - Import from data file into array -
the code below shows should import values temps.txt file times array appears empty , wrong?
i "nan" output.
the temps.txt file looks this:
21.5 22.3 ... etc
here's sourcecode:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>load demo</title> <style> body { font-size: 16px; font-family: arial; } </style> <script src="//code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <script> var times = []; $.get('temps.txt', function(data) { times = data.split("\n"); }); (var in times) { document.write(times[i] + "<br />"); } </script> </html>
something this:
1) move loop $.get
callback.
2) utilize array build string
3) append body element using join
.
$.get('temps.txt', function(data) { times = data.split("\n"); var html = []; (var in times) { html.push(times[i] + '<br/>'); } $('body').append(html.join('')); });
javascript jquery
No comments:
Post a Comment