creativated
creativated

Reputation: 211

stop and restart a looping function in Node js on a specific condition

I have used request npm to get data from an API. In the response I get an object { count : 1 }

If this value is 0 I am sending an email via node mailer.

Due to this it keeps on sending email every 3 seconds but I want to send next email after 600 seconds if the value is still 0.

how can I achieve this without stoping the 3 sec looping.

here is my code

var request = require("request");
function getreq1() {
  request.get('https://api.xxxx.com/xxxxx/xxxxx', {
    oauth: {
        //some code here
    },
    qs: {
        //some code here
    }
  }, function (err, res, body) {
    if (err) throw new Error(err);
    var value = (JSON.parse(body).property['xxxxx'].prop);
    if (value === 0){
        console.log("Sending Alert email");
        sendmail();
    } else{
        console.log("All is well");
    }
  });
};

function init(timer) {

  getreq1();

  setTimeout(function () {
    init(timer)
  }, timer);
}

init(3000);

I want getreq() to run after every 3 sec but function that send email if value===0, after sending an email should not send email for 600 sec if the value is still 0

Upvotes: 1

Views: 114

Answers (2)

An easy way is to have a global variable, considering that you get always from the same url. 600 seconds divided by 3 seconds is 200, thus you can use the Modulus (Remainder) operator %.

var request = require("request");

var timerSum = 0;

function getreq1() {
  request.get('https://api.xxxx.com/xxxxx/xxxxx', {
    oauth: {
        //some code here
    },
    qs: {
        //some code here
    }
  }, function (err, res, body) {
    if (err) throw new Error(err);
    var value = (JSON.parse(body).property['xxxxx'].prop);

    //use Modulus (Remainder), on 0, 200, 400, 600, timerSum%200 === 0 is true
    if (value === 0){
        if(timerSum%200 === 0){
          console.log("Sending Alert email");
          sendmail();
        }
        timerSum++;
    } else if(value === 1){
        console.log("All is well");
        timerSum = 0; //reset
    }
  });
};

function init(timer) {

  getreq1();

  setTimeout(function () {
    init(timer)
  }, timer);
}

init(3000);

I didn't test but it should work. Give some feedback whether it works

Upvotes: 1

user11015087
user11015087

Reputation:

Sounds like you want add a second condition that should only pass once in 600sec.

This can be implemented by storing timestamps and comparing them:

let lastEmailSentAt // add a variable that will store the last timestamp at which condition was fulfilled

function getreq1() {
  // ... your code here
    if (value === 0 && (!lastEmailSentAt || Date.now() - lastEmailSent > 10 * 60 * 1000)) { // 600 sec
      console.log("Sending Alert email");
      sendmail();
      lastEmailSentAt = Date.now();
    } else {
      console.log("All is well");
    }
 // ... your code here
};

Or by setting a boolean variable:

let canSendEmail = true

function getreq1() {
  // ... your code here
    if (value === 0 && canSendEmail) {
      console.log("Sending Alert email");
      sendmail();
      canSendEmail = false;
      setTimeout(function () { canSendEmail = true; }, 10 * 60 * 1000); // 600 sec
    } else {
      console.log("All is well");
    }
 // ... your code here
};

Upvotes: 1

Related Questions