Sampath
Sampath

Reputation: 65870

Set a timer on 10 AM Rxjs/timer

Could you tell me how can I use rxjs/timer to run a method on exact time? I would like to have an initial delay as 1 min and after that, it should run at 10 AM like so. But I was not able to do that. I have done like below. But it fires every 2 mins after 1 min delay. But I need as mentioned above functionality. Any clue or can you tell me another approach maybe?

import { timer } from 'rxjs/observable/timer';

  timer(60000, 120000).subscribe(val => {//schedule Notification
            myMethod();
      });

Upvotes: 0

Views: 252

Answers (1)

tlt
tlt

Reputation: 15211

you can keep running your timer every minute (or whatever precision you need) and trigger it once its 10am. something like:

import { timer } from 'rxjs/observable/timer';

firstRunExecuted:boolean = false;

  timer(60000).subscribe(val => {//schedule Notification
     if(!this.firstRunExecuted)
       {
          myMethod();
          this.firstRunExecuted = true;
       }
      else if(checkIfIts10AM())
          myMethod();
      });

OP's feedback

 timer(60000, 60000).subscribe(val => {//schedule Notification
              const hours = moment().format("H");
              if (hours == '10') this.scheduleNotification();
       });

Upvotes: 1

Related Questions