quo91
quo91

Reputation: 139

SetInterval nested in another setInterval

So I'm trying to program a timer that would execute every 1 hour. The first setInterval should grab a list of items from a web API, then the next timer would execute every second and in that timer you inspect every item individually and make another query if needed. That second timer would end when it reaches the end of the list and it would start again next hour when the first setInterval is being executed. Why do I need 2 timers in the first place? Well simply put I need the second one not to break the API query limit.

My take on this is something like this:

setInterval(function ()
{
    var counter = 0;
    var returnCode;
    var getUrl = "url";
    returnCode = httpGet(getUrl);
    var object = JSON.parse(returnCode);

    function httpGet(url){
      var response = requestSync(
        'GET',
        url, (err, res, body) => {
            if(err)
            {
                return;
            }
        });
        return response.body;
    }
    setInterval(function () 
    {
        //Do some more stuff with data here

        if(!object || typeof object.data[counter] === 'undefined')
        {
            return; //this should end the second setInterval until the first one is activated again
        }
        counter++;
    }, 1000);
}, 3600000);

I know that it doesn't work quite like that because the counter is a local variable and so is the list but I have no idea how to make it work otherwise. Any ideas?

Upvotes: 0

Views: 483

Answers (2)

Anshuman Singh
Anshuman Singh

Reputation: 1152

setInterval(function ()
{
var counter = 0;
var returnCode;
var getUrl = "url";
returnCode = httpGet(getUrl);
var object = JSON.parse(returnCode);

function httpGet(url){
  var response = requestSync(
    'GET',
    url, (err, res, body) => {
        if(err)
        {
            return;
        }
    });
    return response.body;
}
let isFinished = true;
let ref = setInterval(function () 
{
   // if the current iteration is taking longer than 1 second then just return;

   if(!isFinished){
     return;
    }
    isFinished = false
   //Do some more stuff with data here and once it is done make isFinished = true
      isFinished = true;
      counter++;

    if(!object || typeof object.data[counter] === 'undefined')
    {
        return clearTimeInterval(ref); 
    }

   }, 1000);
 }, 3600000);

Upvotes: 1

Orelsanpls
Orelsanpls

Reputation: 23515

You can use a clearInterval to stop your second loop.

/**
 * Perform a GET request and returns JSON body
 */
function httpGet(url) {
  const response = requestSync(
    'GET',
    url,
    (err, res, body) => {
      if (err) {
        return;
      }
    });

  return JSON.parse(response.body);
}

setInterval(function() {
  let counter = 0;

  // Execute one request
  const returnCode = httpGet('url');

  const descriptor = setInterval(function() {
    //Do some more stuff with data here

    if (!object || typeof object.data[counter] === 'undefined') {
      // Stop the second setInterval
      clearInterval(descriptor);

      return;
    }

    counter += 1;
  }, 1000);
}, 3600000);

Upvotes: 1

Related Questions