简体   繁体   English

如何在特定时间使用 rxjs 发出请求?

[英]How to make requests using rxjs at the certain time?

I need to make request three times a day at a certain time (8 AM, 12 AM, 4 PM).我需要每天在特定时间(上午 8 点、中午 12 点、下午 4 点)提出三次请求。 What is the best way to implement this?实现这一点的最佳方法是什么?

This feels a bit like a strange requirement for a JS task and I think you should rather look into making a Cron task for that.这感觉有点像对 JS 任务的奇怪要求,我认为您应该考虑为此创建一个 Cron 任务。 But for the sake of example, here's how you can do it:但为了举例,您可以这样做:

import { of, Observable, timer, EMPTY } from "rxjs";
import { map, filter, timestamp, switchMap } from "rxjs/operators";

type Hour = number;
type Minutes = number;

const atTimes = (times: [Hour, Minutes][]): Observable<[Hour, Minutes]> =>
  timer(0, 1000 * 60).pipe(
    switchMap(() => {
      const date: Date = new Date();
      const [currentHour, currentMinutes] = [
        date.getHours(),
        date.getMinutes()
      ];

      const time = times.find(
        ([hour, minutes]) => hour === currentHour && minutes === currentMinutes
      );

      if (!time) {
        return EMPTY;
      }

      return of(time);
    })
  );


atTimes([[11,48]]).subscribe(x => console.log(x));

Live demo: https://stackblitz.com/edit/rxjs-sxxe41现场演示: https : //stackblitz.com/edit/rxjs-sxxe41

PS: I've assumed that the update doesn't need to be triggered as soon as the new minute starts. PS:我假设不需要在新一分钟开始时立即触发更新。 If that's the case, then timer should tick every second and you should use distinctUntilChanged on the current hour/minutes to wait until they're differents.如果是这种情况,那么计时器应该每秒打勾一次,您应该在当前小时/分钟上使用 distinctUntilChanged 以等待它们不同。

You should look into asyncScheduler from RxJS which uses setInterval for time based operations.您应该查看 RxJS 中的 asyncScheduler,它使用 setInterval 进行基于时间的操作。

Refer to this link: https://rxjs-dev.firebaseapp.com/guide/scheduler请参阅此链接: https : //rxjs-dev.firebaseapp.com/guide/scheduler

Ideally cron jobs are best on the server side to deal with functions need executing at different times, might be worth looking in that as well.理想情况下,cron 作业最好在服务器端处理需要在不同时间执行的功能,可能也值得一看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RxJs:如何进行同步请求? - RxJs: How can I make synchronous requests? 如何使用rxjs以一定的时间间隔从某个范围发出整数 - How to emit integers from certain range with some interval of time using rxjs 使用 RxJS 一次限制请求数 - Limit number of requests at a time with RxJS 使用 rxjs 6 如何创建一个带有缓存的可取消 web 请求管道,并限制同时发生的请求数 - Using rxjs 6 how can I create a cancellable web request pipeline with a cache, and which limits the number of requests happening at the same time Angular, RxJS - 如何使用 switchMap 取消以前的请求? - Angular, RxJS - How to cancel previous requests using switchMap? 如何在使用 forkJoin 触发请求时取消请求 Rxjs、Angular - How to cancel requests incase of triggering them using forkJoin Rxjs , Angular 如何使用 rxjs 处理 http 并发请求和速率限制 - How to handle http concurrent requests and rate limiting using rxjs 如何使用 RxJS 5 无损地对限制请求进行评级 - How do I rate limit requests losslessly using RxJS 5 如何使用 RxJS 重复 HTTP 请求不超过每 N 秒一次 - How to make repeated HTTP requests not more than every N seconds with RxJS 如何使用 RxJS 发出多个 HTTP 请求并将响应合并到一个有效负载中,然后将 map 合并到另一个动作中? - How to make multiple HTTP requests with RxJS and merge the response into one payload and map it to another action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM