Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 发出客户端请求时超过\u查询\u限制_Javascript_Google Maps Api 3 - Fatal编程技术网

Javascript 发出客户端请求时超过\u查询\u限制

Javascript 发出客户端请求时超过\u查询\u限制,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在从客户端请求对25个地址的数组进行地理编码,其中14个地址收到了此错误。这是我的代码,我认为延迟已经足够了: $(function() { var addresses = ["... bunch of addresses..."]; var geocoder = new google.maps.Geocoder(); var outputContainer = $('#resultsContainer'); addresses.forEach(func

我正在从客户端请求对25个地址的数组进行地理编码,其中14个地址收到了此错误。这是我的代码,我认为延迟已经足够了:

$(function() {

    var addresses = ["... bunch of addresses..."];

    var geocoder = new google.maps.Geocoder();
    var outputContainer = $('#resultsContainer');

    addresses.forEach(function(address) {
        setTimeout(function() {
            geocoder.geocode({
                'address': address
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    outputContainer.append('<p>' + address + ' : ' + results[0].geometry.location.A + ' , ' + results[0].geometry.location.k + '</p>');
                } else {
                    outputContainer.append('<p>' + address + ' : Not found, Status: ' + status.toString() + '</p>');
                }
            });
        }, 4000);
    });
})
$(函数(){
var地址=[“…一堆地址…”;
var geocoder=new google.maps.geocoder();
var outputContainer=$(“#resultcontainer”);
地址。forEach(函数(地址){
setTimeout(函数(){
地理编码({
“地址”:地址
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
outputContainer.append(“”+地址+”:“+结果[0].geometry.location.A+”,“+结果[0].geometry.location.k+”

); }否则{ outputContainer.append(“”+地址+”:未找到,状态:“+Status.toString()+”

); } }); }, 4000); }); })

我很可能在这里错过了什么,但我一辈子都想不出此刻发生了什么。有什么建议吗?

当为每个索引设置相同的超时时,在循环中为每个索引设置超时并不能起到很大作用。循环将很快完成(我猜是毫秒),AJAX请求仍将同时发出。谷歌每秒只允许一个请求(或者类似的,我不知道他们允许的确切速率)

如果将较小的超时乘以循环中的当前索引,则可以错开请求。我会从低开始,看看你能做些什么,如果这是用户期待的

这里有一个简单的例子来说明我的意思:

addresses.forEach(function(address, index) {
    setTimeout(function() {
        //some really fun stuff happens here
    }, 500 * index);
});

这将一次运行一个“非常有趣的东西”,每半秒运行一次。第一个索引(0)将没有超时时间,第二个索引将有500ms,第三个索引将有1s,依此类推。

!500毫秒的增量因子似乎还不够。最后几个仍然触发了错误。我把它推到1000,瞧!这里没有用户界面,所以时间并不重要。这不是重复。仅仅因为标题有点匹配,并不意味着同样的问题正在发生。