我试图避免来自 google geocode api 的可怕的查询限制消息。
我从服务器分批获取 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;
}
}