I have a function which receives data from somewhere else which I want to store in an object. I can store it in the object I want perfectly fine, but when I try accessing that object's array property in another file, I keep getting an error that tells me that there is nothing in the array. Here is the code that I am using:
spectrumanalyser.js:
SpectrumAnalyzer.prototype.configSAHandler = function(event)
{
if (this.currComm < saCommands.length)
SAConfigure(this.parent, this.configurl, saCommands[this.currComm++]);
else {
this.ws = SAStream(this.spectrumurl, this);
}
}
which calls
function SAStream(url,sa)
{
ws.onmessage = function (evt) {
//var array = window.atob(evt.data);
if (evt.data instanceof ArrayBuffer) {
var array = new Uint8Array(evt.data);
sa.drawSpectrum(array);
for (var i = 0, j = 0; j < (array.length); i += this.binWidth, j++) {
sa.channelData[j] = array[j]; //This assignment works fine
console.log(sa.channelData[j]); //Logging the data to the console works fine.
}
}
};
The class definition for an sa, or SpectrumAnalyser, object is as follows:
function SpectrumAnalyzer( said, parent, configurl, spectrumurl )
{
this.id = said;
this.parent = parent;
this.configurl = configurl;
this.spectrumurl = spectrumurl;
this.channelData = new Array();
//...rest of the function
}
The main code is contained in current.html:
var sa1 = new SpectrumAnalyzer( 1, 'SA1', "ws://console.sb3.orbit-lab.org:6101", "ws://console.sb3.orbit-lab.org:6100");
// ...some other stuff
for(var i = 0; i < 256; i++)
{
// This is the part that is giving me the error of "undefined"
console.log(sa1.channelData[i]);
}
I'm not exactly sure why the channelData
array is storing values in the spectrumanalyser.js
file, but it is not storing any values in the JavaScript portion of current.html. By the way, the "ws" at the beginning of the URL in the SpectrumAnalyser
constructor indicates that I'm using WebSockets to get my data. Any help would be greatly appreciated. Let me know if I am missing any details, and I will happily provide them.