每15秒运行一次API请求(Running an API Request Every 15 Seconds)

编程入门 行业动态 更新时间:2024-10-18 01:30:08
每15秒运行一次API请求(Running an API Request Every 15 Seconds)

我使用最近速率受限的API,并且必须每15秒运行一次请求,因为这是等待时间,否则返回超过429速率限制的状态代码。

我经常需要针对此API运行多个电子邮件地址,并且电子邮件地址包含在数组中。 如何每隔15.5秒运行一次请求,但是移到每个电子邮件地址直到数组结束? 这肯定是一个非常棘手的问题。 我试过了:

setInterval(checkEmail(email), 15500);

没有快乐,因为某些原因似乎没有用。 顺便说一句,我应该指出我在checkEmail(email)函数中使用了JQuery $ .ajax()。

任何人的想法?

提前致谢。

I use an API that has been recently rate limited and requests have to be run every 15 seconds as that is the wait time otherwise a status code of 429 rate limit exceeded is returned.

I often have more than one email address that needs to be run against this API and the email addresses are contained within an array. How would I go about running the request every say 15.5 seconds but move onto each email address until the end of the array? It's a very tricky one for sure. I've tried:

setInterval(checkEmail(email), 15500);

No joy, for some reason that just doesn't seem to work. Btw should point out that I'm using a JQuery $.ajax() within that checkEmail(email) function.

Any ideas anybody?

Thanks in advance.

最满意答案

您可以执行以下操作:

var emails = []; var interval_id; function _start() { interval_id = window.setInterval(function() { var email = emails.shift(); if (email) { var r=checkEmail(email.address); if (email.callback) email.callback(r); } else { window.clearInterval(interval_id); } }, 15500); } function add_email(email, callback) { emails.push({address:email, callback:callback}); if (!interval_id) _start(); }

每当您要检查新的电子邮件地址时,只需运行add_email(email) 。 这将把它添加到队列中,并在必要时启动间隔计时器。 此外,如果您希望在检查完成时收到通知,您可以说add_email(email,function(result) {....}) 。

You could do the following:

var emails = []; var interval_id; function _start() { interval_id = window.setInterval(function() { var email = emails.shift(); if (email) { var r=checkEmail(email.address); if (email.callback) email.callback(r); } else { window.clearInterval(interval_id); } }, 15500); } function add_email(email, callback) { emails.push({address:email, callback:callback}); if (!interval_id) _start(); }

Whenever you have a new email address to check, just run add_email(email). This will add it to the queue and start the interval timer if necessary. Additionally, you can say add_email(email,function(result) {....}) if you want to get notified when the check is completed.

更多推荐

本文发布于:2023-08-04 23:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API   Running   Seconds   Request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!