Q:

Running an API Request Every 15 Seconds

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.

A:

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.

