2013-03-26 20 views
0

我试图避免从谷歌地理编码api可怕的查询限制消息。避免OVER_QUERY_LIMIT与谷歌地图使用setTimeout,意外的时间差距

我从服务器获取10批次的数据。在一批中,时间安排很好,但批次之间有一个我不明白的延迟。我已经检查了获取新记录的时机,但这只是亚秒而不是问题。

我正在寻找一些关于在哪里创建空隙的指导(理想情况下它们如何移除)。 确切的代码如下。我基本上使用一个稳定在不同批次上的计数器,并且我使用setTimeout,时间为(300*counter),以确保它们不会同时启动。在批次内再次正常工作,但不能超过批次。

不同批次之间的时序是:

start batch 1 :"2013-03-26 15:40:15.882337" 
stop batch 1 :"2013-03-26 15:40:18.586881" 
start batch 2 :"2013-03-26 15:40:21.688363" (3 seconds between batches) 
stop batch 2 :"2013-03-26 15:40:24.384641" 
start batch 3 :"2013-03-26 15:40:30.449829" (6 seconds between batches) 
stop batch 3 :"2013-03-26 15:40:33.150393" 
start batch 4 :"2013-03-26 15:40:42.216579" (9 seconds between batches) 
stop batch 4 :"2013-03-26 15:40:44.917545" 
start batch 5 :"2013-03-26 15:40:56.991258" (12 seconds between batches) 
stop batch 5 :"2013-03-26 15:40:59.689068" 
start batch 6 :"2013-03-26 15:41:14.742534" (15 seconds between batches) 
stop batch 6 :"2013-03-26 15:41:17.444024" 
start batch 7 :"2013-03-26 15:41:35.500424" (18 seconds between batches) 

正如你可以看到批次之间的差距越来越大。

的代码我使用:

getRecords: function(data, textStatus, xhr) { 

    var self = this; 
    var recordsDone = 0; 

    if (data) { 
     for (var i = 0; i < data.geocode.length; i++) { 

     // build the address string 
     var addressQuery = this.buildAddress(data, i); 

     setTimeout(function(addr, recId) { 
      self.googleGeocoder.geocode({'address': addr}, function(result, status) { 
      if (status === google.maps.GeocoderStatus.OK) { 
       $.ajax({ 
       url: '/geocode/' + recId + '.json', 
       data: {'status': status, 'lat': result[0].geometry.location.kb, 'long': result[0].geometry.location.lb}, 
       type: 'PUT', 
       success: function(data, textStatus, xhr) { 
        var total = parseFloat(data.total); 
        var done = parseFloat(data.done); 
        self.set('voortgang', (done/total)*100); 
       }, 
       error: function(xhr, textStatus, errorThrown) { 

       } 
       }); 
      } else { 
       $.ajax({ 
       url: '/geocode/' + recId + '.json', 
       data: {'status': status}, 
       type: 'PUT', 
       success: function(datra, textStatus, xhr) { 
        var total = parseFloat(data.total); 
        var done = parseFloat(data.done); 
        self.set('voortgang', (done/total)*100); 
       }, 
       error: function(xhr, textStatus, errorThrown) { 

       } 
       }); 
      } 

      recordsDone++; 

      if (recordsDone === data.geocode.length) { 
       console.log('about to get records ' + new Date().toUTCString()); 
       $.ajax({ 
       url: '/geocode.json', 
       data: {data_set_id: self.datasetid}, 
       type: 'GET', 
       success: function (data, textStatus, xhr) { 
        console.log('got the records ' + new Date().toUTCString()); 
        self.getRecords(data, textStatus, xhr); 
       }, 
       error: function(xhr, textStatus, errorThrown) { 
        // set to fully geocoded 
        var found = self.get('content').findProperty('id', self.datasetid); 
        Ember.set(found, 'data_set.fully_geocoded', true); 
        self.set(voortgang, 0); 
       } 
       }); 
      } 
      }); 
     }, (300*this.globalRecordsDone), addressQuery, data.geocode[i].id); 

     // another record has been handled 
     this.globalRecordsDone++; 
     } 
    } 

    // get records the first time 
    if (this.firstTime === true) { 
     $.ajax({ 
     url: '/geocode.json', 
     data: {data_set_id: this.datasetid}, 
     type: 'GET', 
     success: function (data, textStatus, xhr) { 
      self.getRecords(data, textStatus, xhr); 
     }, 
     error: function(xhr, textStatus, errorThrown) { 
      alert('stop getting records'); 
     } 
     }); 

     this.firstTime = false; 
    } 

    } 

回答

0

你的问题似乎是在这条线:

}, (300*this.globalRecordsDone), addressQuery, data.geocode[i].id);

我觉得你只是想:

}, (300), addressQuery, data.geocode[i].id);

你正在增加时间的数量在setTimeout等待。第一批,等待3亿美元,第二轮600密尔,第三轮900密尔等等。这就是为什么你要3秒(300密尔乘以10记录= 3秒),然后6秒,600密尔乘以10条记录= 6秒。)

+0

我每次通话后增加globalRecordsDone,每个记录。如果我不这样做,所有10个请求将同时触发,从循环开始时延迟300毫秒。如果我删除* globalRecordsDone,则每条记录都有一个over_query_limit响应。 – Rudi 2013-03-27 07:29:31

0

我将时间定义为循环的函数,为了通过没有超出查询限制的数据集,我必须将超时时间增加到1200。 我认为我之前由于批量间的最小间隔为3秒而停留在查询限制之内。这个过程现在流畅,没有明显的中断。

所以不是

}, (300*this.globalRecordsDone), addressQuery, data.geocode[i].id); 

成为

}, (1200*i), addressQuery, data.geocode[i].id);