Javascript 在数组中循环时发出json请求和存储值的正确方法是什么?
我想通过一个地址数组进行循环,这样我就可以使用GoogleMapsGeocodeAPI请求它们各自的地理位置。我无法将结果(横向/纵向坐标)放入数组。下面的console.log函数似乎是在上面的代码完成执行之前启动的 如果有人能为这个javascript noob指出正确的方向,我将不胜感激Javascript 在数组中循环时发出json请求和存储值的正确方法是什么?,javascript,jquery,arrays,json,ajax,Javascript,Jquery,Arrays,Json,Ajax,我想通过一个地址数组进行循环,这样我就可以使用GoogleMapsGeocodeAPI请求它们各自的地理位置。我无法将结果(横向/纵向坐标)放入数组。下面的console.log函数似乎是在上面的代码完成执行之前启动的 如果有人能为这个javascript noob指出正确的方向,我将不胜感激 function getCoords() { $.each(addressList, function(index, val) { $.getJSON('https:/
function getCoords() {
$.each(addressList, function(index, val) {
$.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + val + '&key=apikey').done(function(data){
coordsList.push(data.results[0].geometry.location);
});
});
console.log(coordsList);
}
您正在混合同步和异步代码。正如您所怀疑的,您的
console.log
在请求完成之前启动
var reqs = []; //set up a container to log our async requests to Google
$.each(addressList, function(index, val) {
reqs.push($.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + val + '&key=apikey').done(function(data){
coordsList.push(data.results[0].geometry.location);
}));
});
//when ALL async requests have completed, console.log.
$.when.apply(this, reqs).done(function() { console.log(coordsList); });
jQuery的延迟对象对这类事情很有好处,也就是说,当一堆异步代码完成时做一些事情
var reqs = []; //set up a container to log our async requests to Google
$.each(addressList, function(index, val) {
reqs.push($.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + val + '&key=apikey').done(function(data){
coordsList.push(data.results[0].geometry.location);
}));
});
//when ALL async requests have completed, console.log.
$.when.apply(this, reqs).done(function() { console.log(coordsList); });
如果您是JS noob,那么对
apply()
的需求就有点超出了这个问题的范围,但本质上它是一种将数组传递给函数的方法,该函数不需要数组,而需要单独的参数$。when()
期望请求作为单独的参数传递,即$。when(req1,req2,…)
但我们的请求在一个数组中<代码>应用()解决了这个问题 您正在混合同步和异步代码。正如您所怀疑的,您的console.log
在请求完成之前启动
var reqs = []; //set up a container to log our async requests to Google
$.each(addressList, function(index, val) {
reqs.push($.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + val + '&key=apikey').done(function(data){
coordsList.push(data.results[0].geometry.location);
}));
});
//when ALL async requests have completed, console.log.
$.when.apply(this, reqs).done(function() { console.log(coordsList); });
jQuery的延迟对象对这类事情很有好处,也就是说,当一堆异步代码完成时做一些事情
var reqs = []; //set up a container to log our async requests to Google
$.each(addressList, function(index, val) {
reqs.push($.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + val + '&key=apikey').done(function(data){
coordsList.push(data.results[0].geometry.location);
}));
});
//when ALL async requests have completed, console.log.
$.when.apply(this, reqs).done(function() { console.log(coordsList); });
如果您是JS noob,那么对
apply()
的需求就有点超出了这个问题的范围,但本质上它是一种将数组传递给函数的方法,该函数不需要数组,而需要单独的参数$。when()
期望请求作为单独的参数传递,即$。when(req1,req2,…)
但我们的请求在一个数组中<代码>应用()解决了这个问题 谢谢你回答我的问题。我尝试运行您在添加apikey后提供的代码,但它似乎不起作用。我没有在控制台中得到任何东西,甚至没有一个空数组。当我将log函数放入.each循环中时,我看到了数据在脚本的顶部需要code>。与您处理reqs
的方法相同。当我将其更改为:$.when.apply($,reqs.done)(function(){console.log(coordsList);})时,它起作用了是的,我错了。很高兴你终于到了。谢谢你回答我的问题。我尝试运行您在添加apikey后提供的代码,但它似乎不起作用。我没有在控制台中得到任何东西,甚至没有一个空数组。当我将log函数放入.each循环中时,我看到了数据在脚本的顶部需要code>。与您处理reqs
的方法相同。当我将其更改为:$.when.apply($,reqs.done)(function(){console.log(coordsList);})时,它起作用了是的,我错了。很高兴你终于到了那里。