setInterval javascript似乎没有很好地响应

setInterval javascript does not seem to respond well

本文关键字:很好 响应 javascript setInterval      更新时间:2023-09-26

小摘要。由于地理编码不允许大量同时请求,并且我必须在我的谷歌地图上显示超过 11 个标记,我想我会使用某种间隔来绕过同时请求限制。
我想在这里我会使用 javascript 中的 setInterval 函数。

这是我的代码

 function timeHackGeocode(location, name, contract, vestigingID)
    {
      setInterval(codeAddress(location, name, contract, vestigingID), 1000);
    }
  function collectingData() {
        @foreach (var item in Model) {
        @:timeHackGeocode("@item.StraatVestiging" + " " + "@item.nr" + "," + "@item.location","@item.name","@item.Contract", "@item.id");
         }        
      }

 function codeAddress(location, name, contract, vestigingID) {
        var address = location;
        var image;
        var zoomlevel; 
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                     var infoboxPos = results[0].geometry.location;
                     var image = new google.maps.MarkerImage(returnImage(contract),
                      // This marker is 20 pixels wide by 32 pixels tall.
                      new google.maps.Size(30, 42),
                      // The origin for this image is 0,0.
                      new google.maps.Point(40,45),
                      // The anchor for this image is the base of the flagpole at 0,32.
                      new google.maps.Point(0, 32));
                     var marker = createMarkers(results[0].geometry.location, contract)
                         google.maps.event.addListener(marker, 'mouseover', function() { 
                         infobox.open(map, marker);
                         infobox.setOptions(infoboxOptions(boxText(name, contract, vestigingID), infoboxPos));
                     });
            } else {
              // alert("Geocode was not successful for the following reason: " + status);
            }   
        });        
    }

不幸的是,这似乎不起作用,我尝试了各种不同的设置。https://developer.mozilla.org/en/DOM/window.setInterval

有人知道发生了什么吗?

附言。我将在未来通过已经拥有纬度和经度来更改此黑客,但现在我希望让它工作。

建议非常感谢。

如果您需要将参数传递给回调函数,但需要它在 Internet Explorer 中工作,而 Internet Explorer 不支持使用 setInterval() 发送其他参数,请使用匿名函数调用回调。

var time =setInterval(function(){ codeAddress(location, name, contract, vestigingID); },1000);

绕过速率限制是可能的。请参阅谷歌地图V3:仅显示一些标记,这是完全相同的问题。

这是该答案的相关部分:

您需要减慢请求速度。当您提交更多请求时,您可能不得不进一步减慢请求速度以满足地理编码器的要求。我在 http://acleach.me.uk/gmaps/v3/plotaddresses.htm 上制作了一个版本 3 示例(来自一个著名的版本 2 示例)——您可以看到它以请求之间的 100 毫秒延迟开始,但在 20 次迭代后需要减慢到 150 毫秒左右。