簡體   English   中英

在上午10點Rxjs /計時器上設置計時器

[英]Set a timer on 10 AM Rxjs/timer

您能告訴我如何使用rxjs/timer在准確的時間運行方法嗎? 我希望有一個1 min的初始延遲,然后,它應該像這樣在10 AM運行。 但是我無法做到這一點。 我已經做了如下。 但延遲1分鍾后,每2分鍾就會觸發一次。 但是我需要如上所述的功能。 有什么線索或者您能告訴我另一種方法嗎?

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

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

您可以每分鍾(或您需要的精度)繼續運行計時器,並在上午10點觸發一次。 就像是:

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的反饋

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM