Access variable in another function

I am trying to access a value that is inside another function. In the example provided I have declared cjs as a global, but it will not provide a value in my alert?

I suspect the problem is that the value is defined inside of geocoder.geocode(

Not sure how exactly to get to that
geocoder = new google.maps.Geocoder();
	var cjs;
	geocoder.geocode( { 'address': address}, function(results, status) { 
	
	
	  if (status == google.maps.GeocoderStatus.OK) {
		  //alert(status);
		   theCoords = results[0].geometry.location;
		   cjs = "hi";
	  } else {
		alert("Please double check to make sure you entered the zip code correctly.");
	  } 
	});
	alert(cjs)

Open in new window

lvmllcAsked:
Who is Participating?
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
you may try using jquery delay() or setTimeOut()
0
 
cmalakarCommented:
I am not sure above Google maps..

But in general javascript  the following attached works.

make sure you >>cjs="hi" is actually being executed.



function() {
        var x;
        xyz = function(){
          x = 2;
        };      
        alert("before invoking the xyz function, x is not initialized yet:" + x);
        xyz();  //invoke function xyz, changes the x value
        alert("Here x value would have changed:" + x); 
    }

Open in new window

0
 
dejaanbuCommented:
if (status == google.maps.GeocoderStatus.OK)  --- this line is not executed means u didnt get a success msg.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
cmalakarCommented:
I believe, the function declared inside gecode itself is not being executed.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
i can't see a global cys just after starting the function at line 3 ?
And you'd better define cjs at first line ... i mean change order of first 2 lines.
0
 
lvmllcAuthor Commented:
when I uncomment //alert(status);

It does alert OK for me so I presume that cjs is getting set to 'hi'


I was wondeirng if the problem is that there is a function inside a function?
0
 
cmalakarCommented:
There are two alerts in above code.. one at line 7 and other at line 14.

Which comes first ?
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
did you add globas as i'd mentioned above as the code below


geocoder = new google.maps.Geocoder();
        var cjs;
        geocoder.geocode( { 'address': address}, function(results, status) { 
          
         global cys;
          if (status == google.maps.GeocoderStatus.OK) {
                  //alert(status);
                   theCoords = results[0].geometry.location;
                   cjs = "hi";
          } else {
                alert("Please double check to make sure you entered the zip code correctly.");
          } 
        });
        alert(cjs)

Open in new window

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Are there any result?
0
 
lvmllcAuthor Commented:
I am using the globals.

what it appears to be doing is sending the return true before the variable gets a chance to get set.  I have been able to test this.

I also tested it by trying to put the value into a field before the return true was executed and the field never changed.

When I set it to return false it worked fine.

so I wonder is there a way to slightly delay the script until the field or variable receives the value  - then return true.
0
 
lvmllcAuthor Commented:
Solution not complete only because I have had to use work around to get it close to functioning.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.