please have a look at the following code. When the value of i == 0 the alert 1 prints variable values as per logic. But if I try to print values (alert 2), it just says "undefined, undefined". My question is what changes I'll have to make to get the values printed in second alert (Alert 2) same as per alert 1?
请看下面的代码。当i == 0的值时,警报1按逻辑打印变量值。但是,如果我尝试打印值(警报2),它只是说“未定义,未定义”。我的问题是,为了使第二次警报(警报2)中打印的值与警报1相同,我必须做出哪些更改?
var testPoint = [];
function load() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(52.5271463402545, -1.50573921491311), 8, G_HYBRID_MAP);
GDownloadUrl("controllers/gmap_genxml2.php", function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
if(i == 0) {
testPoint["lat"] = parseFloat(markers[i].getAttribute("lat"));
testPoint["lng"] = parseFloat(markers[i].getAttribute("lng"));
/********* ALERT 1 ***********/
alert(testPoint["lat"]+" "+testPoint["lng"]);
/********* ALERT 1 End ***********/
}
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(point, name, address, type);
map.addOverlay(marker);
}
});
/********* ALERT 2 ******************/
alert(testPoint["lat"]+" "+testPoint["lng"]);
/********* ALERT 2 Start ***********/
}
}
Thank you for your help. DeeJay
谢谢您的帮助。节目播音员
4 个解决方案
#1
testPoint = [];
// This global var is introduced to mark that testPoint values are not yet loaded.
var isLoaded = false;
function load() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(52.5271463402545, -1.50573921491311), 8, G_HYBRID_MAP);
GDownloadUrl("controllers/gmap_genxml2.php", function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
if(i == 0) {
testPoint["lat"] = parseFloat(markers[i].getAttribute("lat"));
testPoint["lng"] = parseFloat(markers[i].getAttribute("lng"));
/********* ALERT 1 ***********/
alert(testPoint["lat"]+" "+testPoint["lng"]);
/********* ALERT 1 End ***********/
// Set it to true to indicate that testPoint array is already loaded.
isLoaded = true;
}
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(point, name, address, type);
map.addOverlay(marker);
}
});
/********* ALERT 2 ******************/
// Try to alert testPoint each 0.5 sec until we can successfully do it.
function alert2() {
// if testPoint is loaded - then alert it, if not then try in 0.5 sec.
if (isLoaded) {
alert(testPoint["lat"]+" "+testPoint["lng"])
} else {
setTimeout(alert2, 500);
}
};
alert2();
/********* ALERT 2 Start ***********/
}
}
#2
You have to realize that a lot of JavaScript is event based. Here is what is happening:
你必须意识到很多JavaScript都是基于事件的。以下是发生的事情:
GDownloadUrl
takes a callback. The second argument is a function that will be called when the request is complete. It will not be called right away. This is important. After you close the call to GDownloadUrl
, Javascript keeps on going. It doesn't wait for the request to complete. As a matter of fact, if you leave both alerts in you will see that the alert 2 will fire before alert 1. As such, if you want to do a particular thing with these variables once they are fetched, you should move that code to a function and call it from within the GDownloadUrl
callback. This is just the way JavaScript works and you'll get used to it.
GDownloadUrl接受回调。第二个参数是一个在请求完成时将被调用的函数。它不会立即被调用。这个很重要。关闭对GDownloadUrl的调用后,Javascript继续运行。它不等待请求完成。事实上,如果您同时保留两个警报,您将看到警报2将在警报1之前触发。因此,如果您想要在获取这些变量后对这些变量执行特定操作,则应将该代码移至一个函数,并从GDownloadUrl回调中调用它。这就是JavaScript的工作方式,你会习惯它。
#3
The two object keys lat and lng are valued only in the callback function called by GDownloadUrl.
两个对象键lat和lng仅在GDownloadUrl调用的回调函数中被赋值。
You have to wait for it to have executed once, and then you'll have the right values.
您必须等待它执行一次,然后您才能拥有正确的值。
I suggest you to move the alert 2 at the end of this callback function.
我建议你在这个回调函数结束时移动警报2。
#4
You're passing in a function pointer to another function call. That doesn't necessarily make the code execute. It's possible that the timing is off. Your ALERT 1 is happening when the function is called (great), but ALERT 2 is actually being executed before the function is called!
您将函数指针传递给另一个函数调用。这并不一定会使代码执行。时机可能已关闭。调用该函数时,ALERT 1正在发生(很棒),但在调用该函数之前,ALERT 2实际上正在执行!
Try putting in a 1 second delay before printing the contents of testPoint.
在打印testPoint的内容之前,请尝试延迟1秒。
#1
testPoint = [];
// This global var is introduced to mark that testPoint values are not yet loaded.
var isLoaded = false;
function load() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(52.5271463402545, -1.50573921491311), 8, G_HYBRID_MAP);
GDownloadUrl("controllers/gmap_genxml2.php", function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
if(i == 0) {
testPoint["lat"] = parseFloat(markers[i].getAttribute("lat"));
testPoint["lng"] = parseFloat(markers[i].getAttribute("lng"));
/********* ALERT 1 ***********/
alert(testPoint["lat"]+" "+testPoint["lng"]);
/********* ALERT 1 End ***********/
// Set it to true to indicate that testPoint array is already loaded.
isLoaded = true;
}
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(point, name, address, type);
map.addOverlay(marker);
}
});
/********* ALERT 2 ******************/
// Try to alert testPoint each 0.5 sec until we can successfully do it.
function alert2() {
// if testPoint is loaded - then alert it, if not then try in 0.5 sec.
if (isLoaded) {
alert(testPoint["lat"]+" "+testPoint["lng"])
} else {
setTimeout(alert2, 500);
}
};
alert2();
/********* ALERT 2 Start ***********/
}
}
#2
You have to realize that a lot of JavaScript is event based. Here is what is happening:
你必须意识到很多JavaScript都是基于事件的。以下是发生的事情:
GDownloadUrl
takes a callback. The second argument is a function that will be called when the request is complete. It will not be called right away. This is important. After you close the call to GDownloadUrl
, Javascript keeps on going. It doesn't wait for the request to complete. As a matter of fact, if you leave both alerts in you will see that the alert 2 will fire before alert 1. As such, if you want to do a particular thing with these variables once they are fetched, you should move that code to a function and call it from within the GDownloadUrl
callback. This is just the way JavaScript works and you'll get used to it.
GDownloadUrl接受回调。第二个参数是一个在请求完成时将被调用的函数。它不会立即被调用。这个很重要。关闭对GDownloadUrl的调用后,Javascript继续运行。它不等待请求完成。事实上,如果您同时保留两个警报,您将看到警报2将在警报1之前触发。因此,如果您想要在获取这些变量后对这些变量执行特定操作,则应将该代码移至一个函数,并从GDownloadUrl回调中调用它。这就是JavaScript的工作方式,你会习惯它。
#3
The two object keys lat and lng are valued only in the callback function called by GDownloadUrl.
两个对象键lat和lng仅在GDownloadUrl调用的回调函数中被赋值。
You have to wait for it to have executed once, and then you'll have the right values.
您必须等待它执行一次,然后您才能拥有正确的值。
I suggest you to move the alert 2 at the end of this callback function.
我建议你在这个回调函数结束时移动警报2。
#4
You're passing in a function pointer to another function call. That doesn't necessarily make the code execute. It's possible that the timing is off. Your ALERT 1 is happening when the function is called (great), but ALERT 2 is actually being executed before the function is called!
您将函数指针传递给另一个函数调用。这并不一定会使代码执行。时机可能已关闭。调用该函数时,ALERT 1正在发生(很棒),但在调用该函数之前,ALERT 2实际上正在执行!
Try putting in a 1 second delay before printing the contents of testPoint.
在打印testPoint的内容之前,请尝试延迟1秒。