Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

How to properly use global array in java script? *Resoved*

0 votes
asked Jun 17, 2015 in Pset8 by bezerk01 (530 points)
reshown Jun 19, 2015 by Faïza Harbi

 

 

 

 

Turns out $.getJASON is asynchronous!    http://stackoverflow.com/questions/1739800/variables-set-during-getjson-function-only-accessible-within-function

----

 

 

For some reason even tough I declared global arrays--to store markers and info windows--I can not seem to access their values when I want to say:

google.maps.event.addListener(markers[ 0 ], "click", function( ) { 

        info[ 0 ].open(map, markers[ 0 ] );

});

I keep on getting the error "Cannot read property '___e3__' of undefined." So I did console.log to see their values when I did the Event call, and the console showed that both arrays were empty. Below I drew an overview of my code and my calls to console.log as a debugger.  

I assumed that after the function addMarkerWindow gets called I would have values in both arrays, therefore, call the Event listener after addMarkerWindow gets called, right?  

 

script.js

 

var map;

var markers = [ ];

var info = [ ];

$(function( ) {

        //Code

        google.maps.event.addListenerOnce(map,"idle", function( ) {

        configure( );

         // This is where I tried to call the Event listener to open the Info Windows

          // if I call console.log(markers) and consol.log(info) here,

         // it shows markers and info as empty arrays?

         });

       

});

function addMarkerWindow(place)  

        var marker = new google.maps.Marker({ 

        //Code

        });

        var  infowindow = new google.maps.InfoWindow({

        //Code

        });

        info.push(infowindow);

        markers.push(marker);

          //  Just to check, if I call console.log(markers) and consol.log(info) here,

          // it shows markers and info as filled arrays

}

function configure( )

{

        //Code

        update( );

        // This is the next place where I tried to call the Event listener to open the Info Windows

         // if I call console.log(markers) and consol.log(info) here,

         // it shows markers and info as empty arrays?

}

...

function update( )

{

        //Code

        // $.getJASON....   I call addMarkerWindow while scoped in $.getJASON so I can get the data   

                forvar i = 0; i < data.length; i++)

                {

                         addMarkerWindow(data[ i ]);

                         // if I call console.log(marker) and consol.log(info) here, as a sanity check,

                        // it shows markers and info as filled arrays

                 }

        //Code

}

Heres the link for  a better explanation, It shows how the data in my array vanishes after it leaves the scope of "$.getJASON" even though I have a global array? I used console.log to debug. 

http://postimg.org/image/9382k8y1l/full/

http://postimg.org/image/tzfhsjbfh/full/

 

1 Answer

0 votes
answered Jun 19, 2015 by Faïza Harbi (11,960 points)
Good thing you solved it! :)
Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

192 questions

248 answers

217 comments

3,675 users

2 Online
0 Member And 2 Guest
...