How can i reach the array out of document ready function?

asked 2015-05-15 19:04:04 -0600

minerva gravatar image

I've got an array created out of the document ready function, and i need to reach it inside the document ready scopes.

var outarray= [];
var i =0;

var cmdSonarTopic0 = new ROSLIB.Topic({
    ros : ros,
 name : '/gazebo/sensor/Sonar0',
 messageType : 'sensor_msgs/Range'
});

cmdSonarTopic0.subscribe(function(message) {
    outarray.push(message.range);
    console.log(outarray[i]);
    document.getElementById("abc").innerHTML = outarray[1];
    i++;
  });

$(document).ready(function(){
    $("#xyz").text(outarray[1]);
    var inarray= [];
    inarray = outarray; 
    ...
});

<div>
  <p id="abc"></p>
  <p id="xyz"></p>
</div>
  • when i fill the array out of the subscribe function it works, but i need to fill it by datas come from the topic, so it must be in subscribe function.
  • when i do

    document.getElementById("abc").innerHTML = outarray[1];
    

    this in the subscribe function, it works ok. But i need to assign the whole array to another array, so need to receive the array from document ready function.

  • when i try the following, it comes empty and writes nothing in tag.

    $(document).ready(function(){
        $("#xyz").text(outarray[1]);    
    });
    

    So how can i reach the array ?

edit retag flag offensive close merge delete