英文:
setInterval working termination undefined
问题
查看我的代码。<br>
我的 "setInterval" 发送不超过 10 个请求。<br>
然后它停止工作。并且它总是从服务器获得响应。<br>
为什么会发生这种情况,以及如何解决它?我尝试使用 axios 时出现相同的问题。
setInterval(function() {
console.log('我在这里')
fetch(`/api/....`, {
method: 'POST',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
referrerPolicy: 'no-referrer'
}).then((res) => {
console.log('我在这里')
console.log(res)
}).catch((err) => {
console.log(err)
})
}, 1000);
有时也会出现这样的错误:TypeError: 尝试获取资源时的网络错误。
英文:
take a look at my code. <br>
My "setInterval" sends no more than 10 requests.<br>
And it stops working. And it always gets a response from the server.<br>
Why does this happen and how can I fight it? I tried using axios the same problem.
setInterval(function() {
console.log('i'm here')
fetch(`/api/....`, {
method: 'POST',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
referrerPolicy: 'no-referrer'
}).then((res) => {
console.log('I'm here')
console.log(res)
}).catch((err) => {
console.log(err)
})
}, 1000);
Sometimes such an error also flies by:TypeError: NetworkError when attempting to fetch resource.
答案1
得分: 0
这是代码示例,其中使用了Express中间件来实现请求速率限制。你可以通过更改 max
值来限制可用的请求数量。这与 JavaScript 代码无关,JavaScript 代码应该可以在不受限制的情况下触发超过 10 个请求。
文章链接:Rate Limiting
请注意,这段代码示例中的速率限制设置为每 12 小时最多允许 5 个请求。你可以根据需要调整这些值。
英文:
It's hard to said without the server side's codes, but I can guess there is some limitation in your express application which limit the numbers of your requsts to 10.
It is nothing to do with you javascript code which sould fire without any problem more then 10 requests.
As you can see in this article Rate Limiting,
const express = require("express");
const indexRoute = require("./router");
const rateLimit = require("express-rate-limit");
const app = express();
const port = 3000;
app.use(
rateLimit({
windowMs: 12 * 60 * 60 * 1000, // 12 hour duration in milliseconds
max: 5,
message: "You exceeded 100 requests in 12 hour limit!",
headers: true,
})
);
You can limit the available requests by changing the max
value.
答案2
得分: 0
提供你的后端协作的情况下,你的JavaScript代码应该没有问题:
var i = 0,
iv = setInterval(function() {
console.log(`我在这里: ${++i}`);
fetch(`https://jsonplaceholder.typicode.com/users`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ newId: i })
}).then(r => r.json()).then(res => {
console.log(`我回来了: ${res.newId}`);
console.log(res);
if (i == 25) clearInterval(iv);
}).catch((err) => { console.log(err) });
}, 1000);
(你的 console.log
中有一些拼写错误,我已经纠正了它们。)
英文:
Provided your backend co-operates there is no reason why your JavaScript code should not work:
<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
var i=0,
iv=setInterval(function() {
console.log(I'm here: ${++i}
);
fetch(https://jsonplaceholder.typicode.com/users
, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({newId:i})
}).then(r=>r.json()).then(res=>{
console.log(I'm back: ${res.newId}
);
console.log(res);
if(i==25) clearInterval(iv);
}).catch((err) => {console.log(err)});
}, 1000);
<!-- end snippet -->
(There were a few typos in your console.log
s which I straightened out.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论