【发布时间】:2021-01-30 10:24:17
【问题描述】:
我遇到了请求太快的问题。当我的脚本每秒发出太多请求时,Google 会抛出错误 net::ERR_INSUFFICIENT_RESOURCES
我想每 20 毫秒发出一个请求。 我怎样才能做到这一点?
这就是我的主要功能此时的样子......
const symbolsArr = reader.result.split("\n").map((str) => str.trim()); //symbolsArr is just rergular array, except it's source is from .txt file
function loopFunction(error_symbol) {
for (const symbol of symbolsArr) {
setTimeout(getData(symbol), 5000); //I tried to use setTimeout but it not helps
}
return console.log(error_symbol);
}
loopFunction(error_symbols);
还有我的取物器……
error_symbols = [];
function getData(symbol) {
fetch(
`https://cors-anywhere.herokuapp.com/` +
`https://eodhistoricaldata.com/api/fundamentals/${symbol}?api_token= (don't look at my secret token :))`,
{
method: "get",
}
)
.then((res) => {
if (res.ok) {
return res.json();
} else {
throw new Error(`Symbol ${symbol} is 'empty'`);
}
})
.then((data) => {
console.log(data);
var myJSON = JSON.stringify(data);
saveFile(myJSON, `${symbol}-json.txt`, "text/plain");
})
.catch((error) => {
error_symbols.push(symbol);
throw error + symbol;
});
}
很简单,我必须以某种方式对 fetcher 设置冷却时间
【问题讨论】:
-
所以你想要一个 websocket 吗?
-
我的 fetcher 只是从 API 请求数据。浏览器没有足够的时间来处理来自响应的数据。我想在请求之间留一点时间间隔。
-
您是否尝试将 setTimeout 放入 getData 函数中?围绕着承诺?
-
还没有。我试试
-
仍然不起作用,数组中有 1000 个符号 Google 会引发错误。和以前一样
标签: javascript google-chrome request fetch