[英]How do I pass an Array vlaue out of a function using Javascript
I am an absolute JavaScript newbie, however I am working on a private Project. 我绝对是JavaScript新手,但是我正在从事一个私人项目。 In a nutshell I will be reading a few flat files (.csv/.txt format) and passing the values to HighCharts.
简而言之,我将读取一些平面文件(.csv / .txt格式)并将值传递给HighCharts。
I have a handle on nearly everything (I think). 我几乎可以处理所有事情(我认为)。
The only bit I can't get is reading the files into an array, then picking the bits out I need. 我唯一无法得到的就是将文件读入数组,然后从中取出我需要的位。 I can use a script to pass the files straight into Highcharts, but it's not flexible enough for what I need.
我可以使用脚本将文件直接传递到Highcharts中,但是它不够灵活,无法满足我的需求。
Anyway for the below Livedata.txt contains 反正下面的Livedata.txt包含
1370576580,Acidity,pH,8.20,ORP,mV,366.99,ReefTemp,DegC,25.1,RoomTemp,DegC,21.7,HeaterTemp,DegC,25.1 1370576580,酸度,pH,8.20,ORP,mV,366.99,ReefTemp,DegC,25.1,RoomTemp,DegC,21.7,HeaterTemp,DegC,25.1
(EPOCH, then groups of 3 data points (measure, unit, value) for 5 probes) (EPOCH,然后是5个探针的3个数据点(度量,单位,值)的组)
My (the snippet of my) script is: 我的脚本片段是:
var $livedata = new Array();
var currentph;
$.get('livedata.txt', function(data){
livedata = data.split(',');
});
currentph = $livedata[3];
document.write("<b>currentph is </b>=>"+currentph+"<br>");
like this the var currentph = undefined 像这样var currentph = undefined
If I move the document.write statement into the $.get (is this a function?) it does contain the value 8.20 如果我将document.write语句移到$ .get中(这是一个函数吗?),它确实包含值8.20
Furthermore if I declare currentph as 8.20 the rest of my script accepts this value fine. 此外,如果我将currentph声明为8.20,则脚本的其余部分都可以接受此值。
Any help/suggestions are much appreciated. 任何帮助/建议都将不胜感激。
I will be declaring 16 variables, 1 for each value in the array (just for the sake of reading the script later). 我将声明16个变量,其中1个用于数组中的每个值(只是为了稍后阅读脚本)。
Cheers 干杯
Because the $.get
is asyn. 因为
$.get
是异步的。 When you call currentph = $livedata[3];
当您调用
currentph = $livedata[3];
right after that, the response does not arrive yet. 此后,响应尚未到达。 That's the reason why it works when you move the
document.write
statement into the $.get
's callback function. 这就是为什么在将
document.write
语句移至$.get
的回调函数中时起作用的原因。 Also check your typo, you're missing $
before livedata
. 还要检查您的错字,在
livedata
之前缺少$
。 If you need to do syn call, you can try $.ajax
with async:false
如果需要进行同步调用,可以尝试使用
$.ajax
和async:false
$.ajax(
{
url:'livedata.txt',
async:false,
success: function(data){
$livedata = data.split(',');
}
}
);
$
to your variable livedata
. livedata
$
。 $livedata
would be an empty array; $livedata
将为空数组; b) If 'livedata.txt' is loaded, but does not have at least 4 comma separated values, the $livedata[3]
would be undefined. $livedata[3]
将是未定义的。 You may want to re-write your code with something like this: 您可能需要使用以下代码重新编写代码:
var $livedata = []; // array literal notation
$.get('livedata.txt', function(data){
$livedata = data.split(',');
});
// in case array does not have an index 3, set value to 'not found'
var currentph = $livedata[3] || 'not found';
document.write("<b>currentph is </b>=>"+currentph+"<br>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.