我必须检索1000多个ip地址。由于我必须使用ip-api.com / batch,因此此api的post方法要求每分钟最大100 Json参数长度。我输入了超过1000个IP地址的数组,并创建了一个新数组,每个索引的另一个数组的最大长度为100。现在我正在努力使数组的每个索引的http调用都处于等待状态,并等待一分钟等待下一个新请求指数。
//for example I have two index in groups and each index has other 3
indexes
let groups= [[ { query: '193.206.170.145', fields: 'lat,lon' },
{ query: '37.186.207.50', fields: 'lat,lon' },
{ query: '193.206.170.144', fields: 'lat,lon' },
{ query: '62.18.244.163', fields: 'lat,lon' }],
[ { query: '79.52.35.144', fields: 'lat,lon' },
{ query: '62.18.249.56', fields: 'lat,lon' },
{ query: '176.200.132.147', fields: 'lat,lon' }]]
groups.forEach( el => makeRequest(el));
function makeRequest (array){
let toJson = JSON.stringify(array)
setInterval(function(){
axios.post('http://ip-api.com/batch',toJson)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
},60000);
}
Expected:
[{ lat: 45.8, lon: 8.8333 },
{ lat: 45.8155, lon: 8.82753 },
{ lat: 45.8, lon: 8.8333 }]
after one minute
[{ lat: 41.818, lon: 12.4148 },
{ lat: 45.4643, lon: 9.1895 },
{ lat: 45.5324, lon: 9.0402 }]
答案 0 :(得分:0)
const axios = require('axios');
let groups = [[{ query: '193.206.170.145', fields: 'lat,lon' },
{ query: '37.186.207.50', fields: 'lat,lon' },
{ query: '193.206.170.144', fields: 'lat,lon' },
{ query: '62.18.244.163', fields: 'lat,lon' }],
[{ query: '79.52.35.144', fields: 'lat,lon' },
{ query: '62.18.249.56', fields: 'lat,lon' },
{ query: '176.200.132.147', fields: 'lat,lon' }]]
groups.forEach(el => makeRequest(el));
function makeRequest(array) {
for (var i = 0; i < array.length; i++) {
(function (i) {
let toJson = JSON.stringify(array)
setTimeout(function () {
axios.post('http://ip-api.com/batch', toJson)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
})
}, 60000 * i);
})(i);
}
}