Angular 7 Start a function at 09:41 am

Viewed 2053

Hi i use angular 7 and firebase. I just want to write a function that goes off every day at 9:41 am without me having to log in to my application. Is it possible on angular?

3 Answers

You cannot do this with Angular reliably. You want to schedule a cron job to run that firebase function. Take a look at this entry in their blog where they explain one way to do this.


Edit: setInterval approach. Should not be used unless unavoidable.

In theory, you could use setInterval to do this. But it would be finicky.

Say you have an Angular service, and a method. You call this service CronService. In your app component, you can call it, like this:

this.cronService.startScheduledJob();

Now the question is, when to call it.

You would need to trigger the function once at the exact time, 9:41 am. Say you have a button:

<button (click)="startMyJob()" [disabled]="isJobRunning">Start job</button>

Now, your component has this:

const ONE_DAY = 86400 * 1000; // one day has 86400 seconds, but we need milliseconds.

class Component {

  // Property to know if we started:
  isJobRunning = false;
  public startMyJob() {
    if (!this.isJobRunning) {
      this.isJobRunning = true;
    }
    // Now comes your interval part:
    setInterval(() => this.cronService.startScheduledJob(), ONE_DAY);
  }

No - angular works on frontend - so only when user go to your page in the browser (however he not need to log in). You need do it on server

Related