使用setTimeout,意外时间间隔,使用谷歌地图避免OVER_QUERY_LIMIT

时间:2013-03-26 16:08:11

标签: javascript settimeout google-geocoding-api

我正在尝试避免来自Google地理编码API的可怕的查询限制消息。

我从服务器上分批获取10个数据。在一批中,时间是好的,但是在批次之间存在越来越多的我不理解的延迟。我已经检查了获取新记录的时间,但这是亚秒而不是问题。

我正在寻找关于间隙创建位置的一些指导(理想情况下如何删除它们)。 确切的代码如下。我基本上使用一个在不同批次上稳定的计数器,并且我使用时间为(300*counter)的setTimeout来确保它们不会同时触发所有。这在批处理中再次运行良好,但不是批处理。

不同批次之间的时间是:

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;
    }

  }

2 个答案:

答案 0 :(得分:0)

您的问题似乎就在这一行:

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

我认为你只是想要:

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

您正在增加setTimeout等待的时间。第一批,它等待300 mil之间的请求,第二轮600 mils,第三轮900 mils等。这就是为什么你得到3秒(300密尔乘10记录= 3秒。),然后6秒,600密尔乘10记录= 6秒。)

答案 1 :(得分:0)

我将计时作为循环的一个函数,我不得不将超时增加到1200,以便通过没有超出查询限制的数据集。 由于批次之间的最小间隔为3秒,我认为我之前保持在查询限制内。现在流程流畅,没有任何明显的中断。

所以而不是

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

它成了

}, (1200*i), addressQuery, data.geocode[i].id);
相关问题