Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么JavaScript控制台说我的变量未定义,即使我在两行之前就定义了它?_Javascript_Websocket - Fatal编程技术网

为什么JavaScript控制台说我的变量未定义,即使我在两行之前就定义了它?

为什么JavaScript控制台说我的变量未定义,即使我在两行之前就定义了它?,javascript,websocket,Javascript,Websocket,我正在使用WebSocket连接到远程主机,每当我填充realData并将其传递给grapher(),JavaScript控制台总是告诉我realData未定义。我尝试检查数组中的数据类型,但似乎没有问题。在使用包含随机数据的数组之前,我调用了grapher(),调用过程中没有出现任何问题。然而,对于来自WebSocket的数据,调用总是会给我“error:realData未定义”。我不知道为什么会这样。以下是我使用的代码: current.html: var command = "Hi Sco

我正在使用WebSocket连接到远程主机,每当我填充
realData
并将其传递给
grapher()
,JavaScript控制台总是告诉我
realData
未定义。我尝试检查数组中的数据类型,但似乎没有问题。在使用包含随机数据的数组之前,我调用了
grapher()
,调用过程中没有出现任何问题。然而,对于来自WebSocket的数据,调用总是会给我“error:realData未定义”。我不知道为什么会这样。以下是我使用的代码:

current.html:

var command = "Hi Scott"

getData();

function getData()
 { 
    console.log("getData is called");

    if("WebSocket" in window)
    {
            var dataCollector = new WebSocket("ws://console.sb2.orbit-lab.org:6100",'binary');
            dataCollector.binaryType = "arraybuffer";
            console.log(dataCollector.readyState);        

            dataCollector.onopen = function()
            {
                    //alert("The WebSocket is now open!");                  

                    console.log("Ready state in onopen is: " + dataCollector.readyState);


                    dataCollector.send(command);
                    console.log(command + " sent"); 
            }

            dataCollector.onmessage = function(evt)
            {


                            console.log("onmessage is being called");
                            var realData = new Uint8Array(evt.data);
                            console.log(realData);
                            grapher(realData); //everything up to this point works perfectly.
            }

            dataCollector.onclose = function()
            {
                    alert("Connection to Server has been closed");
            }


            return (dataCollector);
    }
    else
    {
            alert("Your browser does not support WebSockets!");
    }
}

graphing.js:

function grapher(realData)
{
            console.log("grapher is called");
            setInterval('myGraph(realData);',1000); //This is where the error is. I always get "realData is not defined".

}

function myGraph(realData)
{
            /*
           for(var i = 0; i < SAarray.length; i++) // Loop which will load the channel data from the SA objects into the data array for graphing.
            {
                    var data[i] = SAarray[i];
            }
            */
            console.log("myGraph is called");

            var bar = new RGraph.Bar('channelStatus', realData);
            bar.Set('labels', ['1','2','3','4','5','6','7','8']);
            bar.Set('gutter.left', 50);
            bar.Set('gutter.bottom', 40);
            bar.Set('ymax',100);
            bar.Set('ymin',0);
            bar.Set('scale.decimals',1);
            bar.Set('title','Channel Status');
            bar.Set('title.yaxis','Status (1 is on, 0 is off)');
            bar.Set('title.xaxis','Channel Number');
            bar.Set('title.xaxis.pos',.1);
            bar.Set('background.color','white');
            bar.Set('colors', ['Gradient(#a33:red)']);
            bar.Set('colors', ['red']);

            bar.Set('key',['Occupied','Unoccupied']);

            bar.getShapeByX(2).Set('colors',barColor(data[0]));


            bar.Draw();


}
函数图示器(realData)
{
log(“称为图示器”);
setInterval('myGraph(realData);',1000);//这就是错误所在。我总是得到“realData未定义”。
}
函数myGraph(realData)
{
/*
对于(var i=0;i
由于传递给
setInterval
的字符串(作为代码)在全局范围内执行,因此
realData
参数不可用。很少有好的理由将字符串传递给
setInterval
。相反,请使用:

setInterval(function () {
    myGraph(realData);
}, 1000);
参考:


在不需要计算字符串的情况下尝试:

setInterval(function() {
    myGraph(realData);
},1000);

无论何时使用
setTimeout
setInterval
,都应该选择传递实际函数而不是字符串。

或使用现代的setInterval(myGraph.bind(realData),1000)@丹达维:不,完全不同。您的解决方案在
myGraph
中设置
this
值,同时它们需要传递
realData
作为第一个参数oops,touche,应该是setInterval(myGraph.bind(this,realData),1000);谢谢你的提醒!完美的我对JavaScript非常陌生,所以当我使用的教程告诉我将字符串传递给setInterval时,我有点困惑。但这一切都解决了,我的问题也解决了。谢谢你的帮助!多么原始。。。哦,等等,我现在明白了,你在粘贴其他人的答案时漏掉了一个空格…@dandavis:相隔5秒…有一百万人在使用,所以,你不认为你偶尔会得到相同的答案吗?