[英]Javascript Callback Within a Callback
I am a stuck as to why I never hit the GETNEARCALLBACK
function below. 对于为什么我从不点击下面的GETNEARCALLBACK
函数,我GETNEARCALLBACK
。 The logic goes something like this: 逻辑如下:
INITIALIZE
在页面加载时,我将其称为INITIALIZE
INITIALIZE
happily executes and calls GETSTATIONS
INITIALIZE
愉快地执行并调用GETSTATIONS
GETSTATIONS
does an AJAX request using GETNEARESTSTAIONS
as the callback function, and the web server responds with the results of a database query in JSON format. GETSTATIONS
使用GETNEARESTSTAIONS
作为回调函数执行AJAX请求,并且Web服务器以JSON格式响应数据库查询的结果。 GETNEARESTSTAIONS
takes the results and creates a Google Maps API distance matrix request using GETNEARCALLBACK
as the callback function GETNEARESTSTAIONS
获取结果并使用GETNEARCALLBACK
作为回调函数创建Google Maps API距离矩阵请求 GETNEARCALLBACK
. 我运行我的网站,并使用Firebug来确定我永远不会进入GETNEARCALLBACK
。 I think my use of Google Maps API is correct because if I don't call GETNEARESTSTATIONS
from within my AJAX request, it executes properly. 我认为我对Google Maps API的使用是正确的,因为如果我未从AJAX请求中调用GETNEARESTSTATIONS
,它将正确执行。
function INITIALIZE() { GETPOSITION(); 函数INITIALIZE(){GETPOSITION(); DRAWMAP(); DRAWMAP(); GETADDR(); GETADDR(); GETSTATIONS(); GETSTATIONS(); } }
var xmlhttp;
function GETSTATIONS() {
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = GETNEARESTSTATION();
xmlhttp.open("GET", "final.php", true);
xmlhttp.send();
}
var STATIONLIST;
function GETNEARESTSTATION() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
STATIONLIST = eval("(" + xmlhttp.responseText + ")");
var LAT = parseFloat(document.getElementById("LATITUDE").value);
var LON = parseFloat(document.getElementById("LONGITUDE").value);
var latlng = new google.maps.LatLng(LAT, LON);
var destinationA = STATIONLIST[0].ADDRESS;
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [latlng],
destinations: [destinationA],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
}, GETNEARCALLBACK);
}
}
function GETNEARCALLBACK(response, status) {
if(status == google.maps.DistanceMatrixStatus.OK) {
var destinations = response.destinationAddresses;
var results = response.rows[0].elements;
for(var j = 0; j < results.length; j++) {
var element = results[j];
document.getElementById("STATIONADDR").innerHTML = parseFloat(element.distance.value) + " " + response.destinationAddresses[j];
}
}
}
need function variables, rather than function return values 需要函数变量,而不是函数返回值
//xmlhttp.onreadystatechange = GETNEARESTSTATION(); //error
xmlhttp.onreadystatechange = GETNEARESTSTATION;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.