setInterval 的工作终止未定义

huangapple go评论62阅读模式
英文:

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(&#39;i&#39;m here&#39;)
    fetch(`/api/....`, {
        method: &#39;POST&#39;,
        headers: {
            &#39;Accept&#39;: &#39;application/json, text/plain, */*&#39;,
            &#39;Content-Type&#39;: &#39;application/json&#39;
        },
        referrerPolicy: &#39;no-referrer&#39;
    }).then((res) =&gt; {
        console.log(&#39;I&#39;m here&#39;)
        console.log(res)
    }).catch((err) =&gt; {
        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(&quot;express&quot;);
const indexRoute = require(&quot;./router&quot;);
const rateLimit = require(&quot;express-rate-limit&quot;);
const app = express();
const port = 3000;

app.use(
  rateLimit({
    windowMs: 12 * 60 * 60 * 1000, // 12 hour duration in milliseconds
    max: 5,
    message: &quot;You exceeded 100 requests in 12 hour limit!&quot;,
    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&#39;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&#39;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.logs which I straightened out.)

huangapple
  • 本文由 发表于 2023年2月8日 13:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定