javascript - jQuery $.when() 函数不起作用
问题描述
我有一小段代码,它将经纬度位置转换为人类可读的地址。我希望在显示人类可读地址之前先进行翻译。这就是为什么我想先使用 $.when() 来完成翻译。
但是,在运行时,不会显示人类可读的地址,仅显示默认值 'xxx'。也没有检测到错误。
var geocoder = new google.maps.Geocoder;
var lat1 = 39.983313,
lon1 = -0.031963;
var addressLatLng = {
lat: parseFloat(lat1),
lng: parseFloat(lon1)
};
var addressHumanReadable = 'xxx';
$.when(
geocoder.geocode({
'location': addressLatLng
}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
addressHumanReadable = results[0].formatted_address;
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
})
).done(function(x) {
alert(addressHumanReadable);
});
解决方案
您不$.when
使用 aDeferred
或 aPromise
或 a来喂它Thenable
,所以它会done
立即触发(更多信息在这里https://api.jquery.com/jquery.when/)
所以这是你的代码......稍微改变了一点,以便它可以执行你的黑暗魔法。
// With the power of Google Maps...
var geocoder = new google.maps.Geocoder;
// .. and some magic numbers!
var lat1 = 39.983313;
var lon1 = -0.031963;
// We shall translate them...
var addressLatLng = {
lat: parseFloat(lat1),
lng: parseFloat(lon1)
};
// ... to something that a Human can process!
var addressHumanReadable = 'xxx';
$.when(
// We execute an anomymous function
// that help us keep things clean
(function(){
// We need a deferred to indicate when we are ready
var isDone = $.Deferred();
// Run your async function
geocoder.geocode(
{'location': addressLatLng},
function(results, status) {
if (status === 'OK') {
if (results[0]) {
addressHumanReadable = results[0].formatted_address;
}
else {
window.alert('No results found');
}
}
else {
window.alert('Geocoder failed due to: ' + status);
}
// Set deferred as resolved
isDone.resolve();
}
);
// Return a jquery deferred
return isDone;
})()
).done(function() {
// MAGIC!
alert(addressHumanReadable);
});
但是等等还有更多!我不喜欢全局变量...我也不喜欢警报中断我的代码流...所以...我们可以删除addressHumanReadable
地理编码中的警报。
// With the power of Google Maps...
var geocoder = new google.maps.Geocoder;
// .. and some magic numbers!
var lat1 = 39.983313;
var lon1 = -23452.031963;
// We shall translate them...
var addressLatLng = {
lat: parseFloat(lat1),
lng: parseFloat(lon1)
};
$.when(
// We execute an anomymous function
// that help us keep things clean
(function(){
// We need a deferred to indicate when we are ready
var isDone = $.Deferred();
// Run your async function
geocoder.geocode(
{'location': addressLatLng},
function(results, status) {
// Check for errors
if (status !== 'OK') {
console.log('Oups... error : ' + status);
isDone.resolve(false);
return;
}
// Check if failed to resolve
if (!results[0]) {
isDone.resolve(false);
return;
}
// No errors... so...
isDone.resolve(results[0].formatted_address);
}
);
// Return a jquery deferred
return isDone;
})()
).done(function(result) {
if (result) {
alert(result);
}
else {
alert('Address not found!');
}
});
快乐的 JavaScript 编码!
推荐阅读
- c# - Guid 应包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) 和有效的 c# 构造函数
- .net - CrystalReport 不使用 .Net 框架
- graphql - Next.js 和 AWS Amplify 出现错误:没有当前用户 - getServerSideProps
- typescript - 如何在 Typescript 中允许“import.meta.*”?
- google-chrome - 有没有办法更改 Chrome 中的默认图标(后退、前进、刷新)
- javascript - 如何在 Fancybox 4 中切换缩略图条的可见性?
- machine-learning - 当时间是一个因素时,创建验证/测试集的最佳方法是什么?
- python - 是否有一种方法可以显示 VotingClassifier 对象内的每个模型的准确度分数?
- kubernetes - Skaffold 没有完全运行所有服务
- php - PHP 如何在一行中调用两个函数?