简体   繁体   English

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

[英]Why is the JavaScript console saying my variable is undefined, even when I defined it two lines before?

I'm using WebSockets to connect to a remote host, and whenever I populate realData and pass it to grapher() , the JavaScript console keeps telling me realData is undefined. 我正在使用WebSockets连接到远程主机,每当我填充realData并将其传递给grapher() ,JavaScript控制台都会告诉我realData是未定义的。 I tried checking the type of the data in the array, but it seems to be fine. 我尝试检查数组中数据的类型,但这似乎很好。 I've called grapher() before using an array with random data, and the call went through without any problems. 在使用带有随机数据的数组之前,我已经调用了grapher() ,并且该调用顺利进行了。 With the data from the WebSocket, however, the call will always give me "error: realData is not defined". 但是,使用来自WebSocket的数据,该调用将始终给我“错误:未定义realData”。 I'm not sure why this is happening. 我不确定为什么会这样。 Here is the code I used: 这是我使用的代码:

current.html: 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: 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();


}

Because strings (as code) passed to setInterval execute in the global scope, therefore the realData parameter isn't available. 由于传递给setInterval字符串(作为代码)在全局范围内执行,因此realData参数不可用。 There's rarely a good reason to pass a string to setInterval . 很少有理由将字符串传递给setInterval Instead, use: 而是使用:

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

Reference: 参考:

Try it without it needing to evaluate a string: 尝试它而不需要评估字符串:

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

Any time you are using setTimeout or setInterval , you should opt for passing an actual function instead of a string. 任何时候使用setTimeoutsetInterval ,都应该选择传递实际函数而不是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控制台错误说变量未定义,即使在同一个 function 中定义 - Console error saying variable not defined, even though defined in same function 控制台说我的数组是未定义的javascript - console saying my array is undefined javascript javascript变量是未定义的甚至定义? - javascript variable is undefined even defined? 为什么javascript中的变量即使定义了,似乎也保持“ undefined”值 - why does a variable in javascript seem to hold the value “undefined” even when it was defined JavaScript抛出TypeError表示我的变量未定义 - JavaScript throws TypeError saying that my variable is undefined 为什么我的外部javascript方法说它是未定义的? - Why is my external javascript method saying it is undefined? 为什么我的function即使我定义了它也返回undefined? - Why does my function return undefined even if I defined it? 当我输入json对象时,firebug控制台为什么总是说“ undefined”? - Why does the firebug console keep saying “undefined” when I enter a json object? React Child 道具变量未定义,即使我之前定义了变量 - React Child prop variable is undefined even though I defined the variable before 为什么我定义的字符串以未定义的形式打印到控制台? - Why are my defined strings printing to the console as undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM