0

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.

rafafan2010
  • 1,559
  • 2
  • 14
  • 23
  • 5
    Are you sure the loop runs *after* `ws.onmessage` was triggered? It seems to me that you are trying to access the data before it was added to the array. – Felix Kling Aug 03 '13 at 20:33
  • I haven't tried that, but I assumed all the SpectrumAnalyser stuff happened as soon as the object was created. I'll do that. By the way, I forgot to add a function that should have been included, but I'm not sure if that helps or not. It's at the top of the page under the spectrumanalyser.js section. – rafafan2010 Aug 03 '13 at 20:47
  • Well, web sockets are asynchronous. `ws.onmessage` is only called once the response was received, not when the object was created. Even though it is about Ajax, have a look at the question [How to return the response from an AJAX call?](http://stackoverflow.com/q/14220321/218196) to learn the difference between asynchronous and synchronous code and how to deal with it. – Felix Kling Aug 03 '13 at 20:52
  • I read about the asynchronous/synchronous issue in the question you posted, and I think I understand the concept. But, I'm not sure how I'd execute that in this case. I will have a lot of SA objects that are receiving data, and need to be able to access the data in each object in order to graph it. What would be the best way to do that? – rafafan2010 Aug 04 '13 at 22:29
  • You could provide a way to add callback to each object. That callback should be executed once it receives its data. – Felix Kling Aug 04 '13 at 22:33

0 Answers0