www.webdeveloper.com
Results 1 to 2 of 2

Thread: Array/Json issue

  1. #1
    Join Date
    Mar 2013
    Posts
    61

    Array/Json issue

    Decided not restart that thread due to this being a different issue. I'm trying to access a variable stored a multidimensional array. For some reason this works fine in example A, but not when I try to do it via a for loop in example B. I've looked into it and from what I see I'm not getting a result in example B. Any thoughts?

    EXAMPLE A:

    Code:
    function tableCreation( info )
    {
    	var table = document.getElementById("tableHolder");
    	
    	geocoder.geocode( { 'address': info[0][1]}, function(results, status){
       	if (status == google.maps.GeocoderStatus.OK) {      
    		alert(results[0].formatted_address);
       } else {
          alert("Geocode was not successful for the following reason: " + status);
       }
    });
    
    	for( var i = 0; i < 3; i++)
    	{
    		var row = table.insertRow();
    		var temp;
    		for(var q = 0; q < 3; q++)
    		{			
    			if(q == 1)
    			{
    				row.insertCell(q).innerHTML = temp;	
    			}
    			else
    			{
    				row.insertCell(q).innerHTML = info[i][q];
    			}
    		}
    	}
    	
    }
    EXAMPLE B:

    Code:
    function tableCreation( info )
    {
    	var table = document.getElementById("tableHolder");
    
    	for( var i = 0; i < 3; i++)
    	{
    		var row = table.insertRow();
    		var temp;
    		for(var q = 0; q < 3; q++)
    		{			
    			if(q == 1)
    			{
    				/*geocoder.geocode( { 'address': info[0][1]}, function(results, status){
       					if (status == google.maps.GeocoderStatus.OK) {      
    						temp = results[0].formatted_address;
       					} else {	
    						alert("Geocode was not successful for the following reason: " + status);
       					}
    				});*/
    				row.insertCell(q).innerHTML = temp;	
    			}
    			else
    			{
    				row.insertCell(q).innerHTML = info[i][q];
    			}
    		}
    	}
    	
    }

  2. #2
    Join Date
    Jul 2011
    Posts
    131
    Example B does not work properly because geocoder.geocode() is async and all other code is sync.
    Try this:
    Code:
    function tableCreation( info )
    {
    	var table = document.getElementById("tableHolder");
    
    	for( var i = 0; i < 3; i++)
    	{
    		var row = table.insertRow();
    		var temp;
    		for(var q = 0; q < 3; q++)
    		{			
    			if(q == 1)
    			{
    				geocoder.geocode( { 'address': info[0][1]}, function(results, status){
       					if (status == google.maps.GeocoderStatus.OK) {      
    						temp = results[0].formatted_address;
       					} else {
    						temp = '';
    						alert("Geocode was not successful for the following reason: " + status);
       					}
    					row.insertCell(q).innerHTML = temp;	
    				});
    			}
    			else
    			{
    				row.insertCell(q).innerHTML = info[i][q];
    			}
    		}
    	}
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles