等待异步Javascript函数的结束来检索结果(延迟?) [英] Wait for the end of an asynchronous Javascript function to retrieve a result (Deferred ?)
问题描述
好的,我知道这个话题已经多次讨论了,但我找不到解决问题的答案。
所以我是试着做一个简单的想法:
我正在创建一个字符串,如:距离为:+ CalculateDistance(position);
想要的结果类似于距离是5kms(8分钟)
。
Ok guys, I know this topic has already been discussed multiple times, but I can't find an answer to solve my problem.
So I'm trying to do a simple think :
I'm creating a string, like : distance is : " + CalculateDistance(position);
The wanted result is something like distance is 5kms (8min)
.
CalculateDistance(position)
是一个调用Google Maps API的函数,名为DistanceMatrix,用于计算两点之间的距离.API记录在案这里,给定的样本完美有效。我按照这样调整:
CalculateDistance(position)
is a function calling a Google maps API called DistanceMatrix to calculate distance between two points. The API is documented here, and the given sample perfectly works. I adapted it like this :
function CalculateDistance(position)
{
var service = new google.maps.DistanceMatrixService();
var destination = new google.maps.LatLng(/* some lat/lng */);
service.getDistanceMatrix(
{
origins: [position],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status) {
if (status == google.maps.DistanceMatrixStatus.OK)
{
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var results = response.rows[0].elements;
distanceMatrixResult = results[0].distance.text + " ( " + results[0].duration.text + " min)";
}
哦顺便说一下, distanceMatrixResult
是一个全局变量。实际上,我只需要获取结果[0] .distance.text的内容(当我在控制台中以 callback()
打印它时,值为OK )然后将值与Distance is连接起来。如果有人有更智能的解决方案,欢迎使用!
Oh, by the way, distanceMatrixResult
is a global variable. In fact, I just need to get the content of results[0].distance.text (and when I print it in the console in callback()
, the value is OK) and then concatenate the value with "Distance is". If someone have a smarter solution, it is welcomed !
API调用异步,在我的情况下, distanceMatrixResult
在结果字符串中始终为空。但是当我在控制台中记录它的值时, distanceMatrixResult
值变成了一个好的 AFTER 字符串的创建。
The API call is asynchronous, and in my case, distanceMatrixResult
is always empty in the result string. But when I log its value in the console, distanceMatrixResult
value became the good one AFTER the string was created.
我想写的是:
•调用 CalculateDistance
(异步调用回调)
•当 callback
结束时,将字符串与 distanceMatrixResult
值连接。
• Call CalculateDistance
(which call callback asynchronously)
• When callback
is ended, concatenate the string with the distanceMatrixResult
value.
我尝试了一些延期,例如 $。完成()
和 $。when()。then()
但我不能成功......
I tried some Deferred, like $.done()
and $.when().then()
but I can't success...
有人能帮帮我吗?
谢谢!
Can someone help me ?
Thank you !
推荐答案
distanceMatrixResult
是一个全局变量
它不应该,它应该是您的承诺(延期)所代表的价值。这是基本设置:
It should not, it should be the value which your Promise (Deferred) stands for. This is the basic setup:
function calculateDistance(position) {
var service = new google.maps.DistanceMatrixService();
var destination = new google.maps.LatLng(/* some lat/lng */);
var dfd = $.Deferred();
service.getDistanceMatrix({
origins: [position],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
if (status == google.maps.DistanceMatrixStatus.OK)
dfd.resolve(response);
else
dfd.reject(status);
});
return dfd.promise();
}
现在,你要做
calculateDistance(…).then(function(response) {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var results = response.rows[0].elements;
return results[0].distance.text + " ( " + results[0].duration.text + " min)";
}).done(function(distanceMatrixResult) {
var myString = "distance is: "+distanceMatrixResult;
// do something with your string now
});
这篇关于等待异步Javascript函数的结束来检索结果(延迟?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!