2014-04-01 170 views
0

我在下面的代码中遇到了分配给全局变量(成功,count_error,错误)的最终值的问题。在输出部分之前如果我不包括“alert(成功)”;所有的值都是零。但是,如果我包含该行,则会输出正确的值。 为什么这是变量作用域有问题?Jquery全局变量范围/错误值

<html> 
<head> 

<script src="../jquery-1.11.0.js"></script> 
<script> 
var rows_all = [1,2,3,4,5,6,7,8,9,10], 
    success = 0, 
    count_error = 0, 
    error = []; 

///////////////////////////////////////////////////////////////////////// 
// In test_addresses create lat/lon [number] from coordinates [string] // 
///////////////////////////////////////////////////////////////////////// 
$.getJSON("http://******.cartodb.com/api/v2/sql?q=" + "SELECT cartodb_id FROM test_addresses" + "&api_key=******", function(data) { 

//get #rows for loop function 
//$.each(data.rows, function(key, val) { 
    //rows_all.push(val['cartodb_id']); 
//}); 


//loop through rows (get coordinates), manipulate + post values 
$.each(rows_all, function(key1, val1) { 
    $.getJSON("http://******.cartodb.com/api/v2/sql?q=" + "SELECT address, coordinates FROM test_addresses WHERE cartodb_id=" + val1 + "&api_key=******", function(data1) { 
     $.each(data1.rows, function(key2, val2) { 
      address = val2['address']; 
      lat_lon = val2['coordinates']; 
      if (lat_lon.indexOf('?') === -1) { 
       lat = parseFloat(lat_lon.split(',')[0]); 
       lon = parseFloat(lat_lon.split(',')[1]); 
       $.post("http://******.cartodb.com/api/v2/sql?q=" + "UPDATE test_addresses SET lat=" + lat + ", lon=" + lon + "WHERE cartodb_id=" + val1 + "&api_key=******"); 
       success++; //number of successfully completed operations 
      } 
      else { 
       count_error++; //#error operations 
       list = {}; 
       list["id"] = val1; //@which cartodb_id in table 
       list["address"] = address; //@which matching address in table 
       error.push(list); 
      } 
     }); 
    }); 
}); 

alert(success); 
//Ouput text  
$("#result").html(success + " entries successfully geocoded. </br><br>There were " + count_error + " errors. <br>More pecifically at cartodb_id : address:"); 
$.each(error, function(key4, val4) { 
    $("#result").append("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + val4["id"] + " : " + val4["address"]); 
}); 

$.each(rows_all, function(key5, val5) { 
    $("#result").append("<br>" + key5); 
}); 
}); 

</script> 
</head> 

<body> 
    <p id="result"></p> 
</body> 
</html> 

回答

0

这是一个异步请求,所以alert()将在获取数据之前触发。所以,你应该改变这样的代码,

$.getJSON("http://******.cartodb.com/api/v2/sql?q=" + "SELECT cartodb_id FROM test_addresses" + "&api_key=******", function (data) { 

//get #rows for loop function 
//$.each(data.rows, function(key, val) { 
//rows_all.push(val['cartodb_id']); 
//}); 


//loop through rows (get coordinates), manipulate + post values 
$.each(rows_all, function (key1, val1) { 
    $.getJSON("http://******.cartodb.com/api/v2/sql?q=" + "SELECT address, coordinates FROM test_addresses WHERE cartodb_id=" + val1 + "&api_key=******", function (data1) { 
     $.each(data1.rows, function (key2, val2) { 
      address = val2['address']; 
      lat_lon = val2['coordinates']; 
      if (lat_lon.indexOf('?') === -1) { 
       lat = parseFloat(lat_lon.split(',')[0]); 
       lon = parseFloat(lat_lon.split(',')[1]); 
       $.post("http://******.cartodb.com/api/v2/sql?q=" + "UPDATE test_addresses SET lat=" + lat + ", lon=" + lon + "WHERE cartodb_id=" + val1 + "&api_key=******"); 
       success++; //number of successfully completed operations 
       alert(success); 

      } else { 
       count_error++; //#error operations 
       list = {}; 
       list["id"] = val1; //@which cartodb_id in table 
       list["address"] = address; //@which matching address in table 
       error.push(list); 
      } 
     }); 
     //Ouput text  
     $("#result").html(success + " entries successfully geocoded. </br><br>There were " + count_error + " errors. <br>More pecifically at cartodb_id : address:"); 
     $.each(error, function (key4, val4) { 
      $("#result").append("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + val4["id"] + " : " + val4["address"]); 
     }); 

     $.each(rows_all, function (key5, val5) { 
      $("#result").append("<br>" + key5); 
     }); 
    }); 
}); 



}); 

你应该把代码的$.getJSON本身的范围内。所以它只会在获取数据后才能运行。

其实它不alert()做魔术。如果您发出警报,则在用户单击确定按钮之前,成功事件将以较少的时间间隔发生。在那段时间内,所有的价值都将被填充。