rxjs中是否有在一天中的特定时间调用函数的默认方法



我正在努力寻找解决方案,但没有找到。

使用javascript我能够实现这一点。请建议rxjs中是否有默认函数来实现以下要求。

需求每天10:00我想做一个API调用并刷新现有的数据。

提前谢谢。

没有特殊的函数可以在一天中的特定时间调用方法,但您可以使用timer倒计时到该时间,然后每24小时发出一次。

然而,为了防止潜在的夏令时调整,最好每小时发射一次,并在您想要的时间内发射filter

const msPerHour = 1000 * 60 * 60;
const msUntilNextHour = msPerHour - new Date().getTime() % msPerHour;
// emit every hour at 0 minutes, 0 seconds
const onHour$ = timer(msUntilNextHour, msPerHour).pipe(
map(() => new Date())
);
const dailyAt10am$ = onHour$.pipe(
filter(now => now.getHours() === 10)
);
dailyAt10am$.subscribe(
() => console.log("It's 10:00, let's do some work...")
);

这里有一个StackBlitz的例子。

最新更新