我正在尝试从单独的函数获取var newtotal的值,然后通过Ajax将值传递给PHP脚本。
这是我的代码:`
function customerCost(){
var newtotal;
var start = document.getElementById('source').value;
var end = document.getElementById('destination').value;
var via = document.getElementById('via').value;
if(via != ""){
var waypts = [];
waypts.push({
location:via,
stopover:false});
var newrequest = {
origin: start,
destination: end,
waypoints: waypts,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(newrequest, function(newresponse, newstatus) {
if (newstatus == google.maps.DirectionsStatus.OK) {
var newtotal = 0;
var mynewroute = newresponse.routes[0];
for (i = 0; i < mynewroute.legs.length; i++) {
newtotal += mynewroute.legs[i].distance.value;
}
return newtotal;
}
});
}else{
var newrequest = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(newrequest, function(newresponse, newstatus) {
if (newstatus == google.maps.DirectionsStatus.OK) {
var newtotal = 0;
var mynewroute = newresponse.routes[0];
for (i = 0; i < mynewroute.legs.length; i++) {
newtotal += mynewroute.legs[i].distance.value;
}
return newtotal;
}
});
}
}
然后我在一个单独的函数中使用以下函数调用该值:
customertotal = customerCost();
我只收到“未定义”错误。但是,如果我将return newtotal;
替换为alert(newtotal);
,则会输出正确的值。
我哪里错了?
答案 0 :(得分:2)
directionsService.route()
调用是异步的。这意味着当你调用它时,它只会启动操作,你的customerCost()
函数会继续并在.route()
ajax调用完成之前很久就结束并调用它的回调。
您正尝试使用异步操作以同步方式进行编程。你不能这样做。您的customerCost()
函数调用无法返回newtotal
值,因为在函数返回时尚未知道它。
相反,您需要学习如何以异步方式编程。任何需要newtotal
值的代码都需要位于该值可用的回调中,或者您可以从那里调用函数并将newtotal
值传递给它。
开发类似这样的东西的典型方法可能是你自己的回调:
function customerCost(cb){
var newtotal;
var start = document.getElementById('source').value;
var end = document.getElementById('destination').value;
var via = document.getElementById('via').value;
if(via != ""){
var waypts = [];
waypts.push({
location:via,
stopover:false});
var newrequest = {
origin: start,
destination: end,
waypoints: waypts,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(newrequest, function(newresponse, newstatus) {
if (newstatus == google.maps.DirectionsStatus.OK) {
var newtotal = 0;
var mynewroute = newresponse.routes[0];
for (i = 0; i < mynewroute.legs.length; i++) {
newtotal += mynewroute.legs[i].distance.value;
}
// call the callback and pass it the newtotal
cb(newtotal);
}
});
}else{
var newrequest = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(newrequest, function(newresponse, newstatus) {
if (newstatus == google.maps.DirectionsStatus.OK) {
var newtotal = 0;
var mynewroute = newresponse.routes[0];
for (i = 0; i < mynewroute.legs.length; i++) {
newtotal += mynewroute.legs[i].distance.value;
}
// call the callback and pass it the newtotal
cb(newtotal);
}
});
}
}
// call the asynchronous cost function and process the result
// in a callback function
customerCost(function(newtotal) {
// the newtotal value is available here
});
答案 1 :(得分:1)
你应该使用回调函数来处理异步函数中的变量。
您可以添加回调函数作为参数:
customerCost(callback){
// your original code
}
然后将所有行return newtotal
替换为callback(newtotal);
然后调用函数customerCost
,如:
customerCost(function(customertotal){
// work with customertotal
});