Home > Mobile >  How to call a function by setting a time period
How to call a function by setting a time period

Time:10-02

I want to achieve a process where by I update my firestore database after every new month.

So I am having this function below

export const updateSubPlan = (uid) => async (dispatch) => {
  const subplan = 2
  var userRef = db.collection("users").doc(uid);
   userRef.update({
      plan: subplan,
  })
  .then(() => {
    console.log('Sub plan updated');
  })
  .catch((error) => {
    var errorMessage = error.message;
    console.log('Error updating subplan:', errorMessage);
  });
}

I want the updateSubPlan function to be automatically triggered once after every new month.

So let's say it is triggered today which is 1/10/2022, the next time it will be triggered will be 1/11/2022.

how can I achieve this functionality in react.js

CodePudding user response:

You can do this way!

Just call your function if isFirstDayOfMonth is true

let isFirstDayOfMonth =  new Date().toISOString().slice(8, 10) === "01";

isFirstDayOfMonth 
     ? console.log( "Yes, Today is first day of month" )
     : console.log("No, Today date is "   isFirstDayOfMonth )

CodePudding user response:

you can use his external library https://www.npmjs.com/package/react-js-cron

  • Related