javascript – JS全局变量未在第一次迭代时设置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – JS全局变量未在第一次迭代时设置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6033字,纯文字阅读大概需要9分钟。
内容图文
![javascript – JS全局变量未在第一次迭代时设置](/upload/InfoBanner/zyjiaocheng/703/b0f60e55f0b8418d961bb2ee38f63b39.jpg)
我正在尝试在处理一些代码后为全局变量赋值.它没有按计划运作.
我所做的是在两个文本框中输入地址和城市.它进入最后一个函数并调用codeAddress获取地址的坐标.
从那里我发送坐标到setLatLng,它工作正常.但我无法使用getLatLng调用longlats来查看设置值.
如果我添加地址和城市两次,它只会显示一个值.我认为longlats的初始化时间太晚,以至于我没有及时看到正确的值.
有什么建议?
相关代码如下.
<script>
var longlats ="";
function setLatLng(x,y){
longlats = x+","+y;
alert(longlats) //shows value
}
function getLatLng(){
alert(longlats);
return longlats;
//calling this gives undefined
}
function codeAddress() {
$('#map').show();
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value +"," + document.getElementById("city").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
locationsall[counter] = new Array();
locationsall[counter][0] = address;
locationsall[counter][1] = lat; //has value
locationsall[counter][2] = lng; //has value
setLatLng(locationsall[counter][1],locationsall[counter][2]);
counter++;
}
$(function() {
$("#locationadd").click(function() {
codeAddress();
var get = getLatLng();//value is undefined..
//i want to do stuff with the values in longlats
}
)};
</script>
EDIT1
$("#locationadd").click(function() {
var address = $("#address").val();
var city = $("#city").val();
if (address =="" || city =="") {
$("#locationtext").text('*Please enter an address and city');
return false;
}
else {
codeAddress(function(){
var get = getLatLng();
var longlat = get.split(",");
var lat = longlat[0];
var lng = longlat[1];
var $tr2 = $('<tr>').css({
'padding': '4px',
'background-color': '#ddd'
}).appendTo('#locationDeals');
var location = [
address,
city
];
locations.push(location);
for (i = 0; i < 2; i++) {
var $td2 = $('<td>').appendTo($tr2);
$('<span>').text(location[i]).appendTo($td2);
$('<input type="hidden">').attr({
'name': 'loc[' + ctr + '][' + i + ']',
'value': location[i]
}).appendTo($td2);
}
var $tdRemoveRow2 = $('<td>').appendTo($tr2);
$('<a>').attr({
'href': '#',
'id': 'submit-button'
}).text("Remove").click(function() {
var index2 = $tr2.index();
$("#locationDeals input[type='hidden']").each(function() {
this.name = this.name.replace(/\[(\d+)\]/, function(str2, number2) {
return "[" + (+number2 > index2 - 1 ? number2 - 1 : number2) + "]";
});
});
$tr2.remove();
var tmp = locations.splice(locations.indexOf(location), 1);
deleteMarker(tmp);
return false;
}).appendTo($tdRemoveRow2);
ctr++;
document.getElementById("address").value = "";
document.getElementById("city").value = "";
document.getElementById("address").focus();
$("#locationtext").text('');
return false;
});
}
});
解决方法:
问题是geocoder.geocode是异步的.你不能这样做,因为你的codeAddress函数将在设置longlats之前返回.只有在谷歌地图返回结果后,您才需要更改编码样式以使用longlats.
您需要更改以下代码:
$("#locationadd").click(function() {
codeAddress();
var get = getLatLng();//value is undefined..
//i want to do stuff with the values in longlats
}
对此:
$("#locationadd").click(function() {
codeAddress(function(){
var get = getLatLng();
//do stuff with the values in longlats
});
});
为此,您需要将copyAddress更改为:
function codeAddress(callback) {
/*
* Some stuff
*/
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
/*
* Some stuff
*/
setLatLng(locationsall[counter][1],locationsall[counter][2]);
callback(); // This is where/when the values are returned
// so this is where/when we want to execute code.
}
})
}
补充说明:
根据您的评论,您似乎仍在尝试这样做:
var val;
codeAddress(function(){
val = getLatLng();
});
doStuffWith(val);
这不起作用,因为codeAddress返回BEFORE执行getLatLng.您需要更改编码的样式.你需要像这样写:
codeAddress(function(){
val = getLatLng();
doStuffWith(val);
});
也就是说,您需要将调用codeAddress之后编写的所有代码移动到函数INSIDE codeAddress.
原因是代码执行的方式是这样的:
>浏览器输入JAVASCRIPT阶段:
codeAddress is called
|
|
|--------------> inside codeAddress a request to google maps
| is made which registers a callback function
| to be executed once it returns.
v
codeAddress returns
note that at this point getLatLng isn't called yet
this is because the browser haven't made the request
to google maps yet because it's still executing the
javascript engine.
|
v
code written after codeAddress is executed
(remember, the call to google maps still haven't happened yet)
|
v
no more javascript to execute
> javascript阶段结束,浏览器输入DOM渲染阶段
The DOM gets updated and drawn on screen if there are any changes.
> DOM渲染结束,浏览器进入网络阶段
(note: in reality the network layer is asynchronous)
Browser executes pending requests (images, iframes, XMLHttprequest etc.)
Makes the necessary network connections and begins downloading stuff.
|
|
v
Browser services completed requests and schedules appropriate events
(update DOM, trigger onreadystatechange etc.)
Note that at this point no javascript or DOM updates actually happens.
The browser does things in a loop, not recursively.
It is at this point that the google maps request is made.
|
v
No more network operations to service
>网络阶段结束,浏览器输入JAVASCRIPT PHASE
google maps request completes, onreadystatechange executes
|
|
|----> inside onreadystatechange the callback to the
| google maps API is called
| |
| '-----> inside the google maps callback getLatLng
| is called, then the callback to codeAddress
| is called.
| |
| '------> inside the callback to
| codeAddress you can now
| use the returned values.
| This is the reason you need
| to move all your code here.
v
no more javascript to execute
> javascript阶段结束,浏览器输入DOM渲染阶段
并且事件循环继续.
如您所见,您不能简单地从codeAddress返回值.相反,您需要在codeAddress中移动代码以使其在正确的时间执行.
内容总结
以上是互联网集市为您收集整理的javascript – JS全局变量未在第一次迭代时设置全部内容,希望文章能够帮你解决javascript – JS全局变量未在第一次迭代时设置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。