我使用最近速率受限的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.
更多推荐
发布评论