Javascript jquery在for循环中延迟

Javascript jquery在for循环中延迟,javascript,jquery,google-maps,jquery-deferred,Javascript,Jquery,Google Maps,Jquery Deferred,因此,我一直在研究jquery,但在循环中检索数据时遇到了问题。延迟部分似乎只处理来自最终迭代的数据。如果数组中只有一个项目,那么它也会失败,因此我不确定发生了什么 我有不同的城市名称,我正试图从谷歌地图反向地理编码中获取每个城市的中心坐标 下面是我的函数,它获取中心坐标: function getGroupLatLng(groupname){ var deferred = new $.Deferred(); geocoder.geocode( { 'address': gr

因此,我一直在研究jquery,但在循环中检索数据时遇到了问题。延迟部分似乎只处理来自最终迭代的数据。如果数组中只有一个项目,那么它也会失败,因此我不确定发生了什么

我有不同的城市名称,我正试图从谷歌地图反向地理编码中获取每个城市的中心坐标

下面是我的函数,它获取中心坐标:

function getGroupLatLng(groupname){
    var deferred = new $.Deferred();

     geocoder.geocode( { 'address': groupname}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
              deferred.resolve(results);

          alert(results[0].geometry.location.lat());
      } else {

      }

    });
    return deferred.promise();
}
这是调用函数的地方,返回结果后会追加一个div:

var newGroupsLength = newGroups.length;

for (i = 0; i < newGroupsLength; i++) {

    newGroups[i]['counter']=counter;
    var locationName = newGroups[i]['name'];
    counter++;
    alert(locationName);
    $.when(getGroupLatLng(locationName)).then(function(results){
        alert("lat = "+results[0].geometry.location.lat());
        var lat=results[0].geometry.location.lat();
        var lng=results[0].geometry.location.lng();
        console.log(newGroups[i]); //this is running the proper number of times, but it logs the results from the final looped item, 'n' number of times. 

        newGroups[i]['lat']=lat;
        newGroups[i]['lng']=lng;

        var jsonArray=[];
        jsonArray = newGroups[i];
        var template = $('#groupsTemplate').html();
        var html = Mustache.to_html(template, jsonArray);
        $('#groups-container').append(html);
    });
}
var newGroupsLength=newGroups.length;
对于(i=0;i
我遇到的问题是,延迟循环似乎处理for循环中最后一项的次数为“n”,其中“n”是newGroupsLength数组中的项数。当然,它应该处理每个项目一次。如果删除了延迟操作,则一切正常


衷心感谢您的帮助。非常感谢

有两个事实可以合作得出这一结果:

  • 将对象写入日志时,写入的是对对象的引用,而不是数据的副本。如果对象在记录后发生更改,日志将显示更改的数据

  • 在第一次调用
    的回调函数之前,循环将已经完成。这意味着
    i
    对所有回调都具有相同的值,因此将所有结果放在同一个对象中

  • 因此,
    newGroups[i]
    中的值对于处理的每个响应都会更改,但您只能在日志中看到最后的值,因为在日志显示时,对象包含的就是最后的值

    要使循环中的每个迭代都保留
    i
    的值,以备以后响应到达时使用,可以使用IIFE(立即调用的函数表达式)为每个迭代创建局部变量:

    var newGroupsLength = newGroups.length;
    
    for (i = 0; i < newGroupsLength; i++) {
    
      (function(i){
    
        newGroups[i]['counter']=counter;
        var locationName = newGroups[i]['name'];
        counter++;
        alert(locationName);
        $.when(getGroupLatLng(locationName)).then(function(results){
            alert("lat = "+results[0].geometry.location.lat());
            var lat=results[0].geometry.location.lat();
            var lng=results[0].geometry.location.lng();
    
            newGroups[i]['lat']=lat;
            newGroups[i]['lng']=lng;
    
            console.log(newGroups[i]); // log the object after setting the values
    
            var jsonArray=[];
            jsonArray = newGroups[i];
            var template = $('#groupsTemplate').html();
            var html = Mustache.to_html(template, jsonArray);
            $('#groups-container').append(html);
        });
    
      })(i);
    
    }
    
    var newGroupsLength=newGroups.length;
    对于(i=0;i
    wow。非常感谢。console.log特性非常有意义。我不知道,log就是这么做的。最初,函数(i){}中的任何内容都没有运行,但我会继续处理它,并在我这方面与您联系。非常感谢你。。。虽然我认为问题已经解决了,但解释是无价的。