2013-03-26 59 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);