使用RXJS和Angular 14起,毫秒柜台

发布于 2025-02-12 17:05:29 字数 636 浏览 1 评论 0原文

我正在尝试使用Angular 14项目上的RXJ从特定日期设置计数器,但计数器似乎是错误的。 毫秒仅在红色点上变化,几秒钟在橙色点上发生变化,但是几秒钟应该在蓝点变化,而毫秒应计算在橙色和红色斑点上。我正确吗?

我在此代码中的错误是什么?

TS:

startTime = new Date("April 10, 2013 00:00:00").getTime();
elapsedTime = Date.now() - this.startTime;
count: Observable<number> | undefined;
    
this.count = interval()
  .pipe(map(count => count + this.elapsedTime)
  );
}

这是占位符:

{{ count | async }}

谢谢

I am trying to setup a counter from a specific date using RxJS on an Angular 14 project, but the counter seems wrong.
Milliseconds only change on the red spot, seconds are changing on the orange spot, but seconds should change at the blue spot, and milliseconds should countup on the orange and red spot combined. Am I correct?

enter image description here

What is my mistake in this code?

TS:

startTime = new Date("April 10, 2013 00:00:00").getTime();
elapsedTime = Date.now() - this.startTime;
count: Observable<number> | undefined;
    
this.count = interval()
  .pipe(map(count => count + this.elapsedTime)
  );
}

This is the placeholder:

{{ count | async }}

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2025-02-19 17:05:30

问题在于,您正在使用间隔安排每个MS的新通知,但是应用程序无法处理并在1M中渲染每个通知。这意味着随着时间的流逝,通知计数是过程(您的预定量度指标)和MS中真正经过的时间之间的差异变得越来越大,因此时间似乎较慢。

防止这种情况的最简单方法是增加排放之间的间隔时间,以避免发出积压的通知,并计算出每次迭代中的启动时间的大轨时间。

this.count = interval(30).pipe(
  map(count => Date.now() - this.startTime)
);

干杯

The problem is that you are scheduling with interval a new notification every ms, but the app cant process and render each notification in 1m. That means that as time goes by, the difference between the notification count being process (your intended measure of elapsedTime) and the real elapsed time in ms gets bigger and bigger and thus the time seems to run slower.

The easiest way to prevent this is increase the interval time between emissions to avoid a backlog of notifications, and to calculate the elapsedTime against the startTime in each iteration.

this.count = interval(30).pipe(
  map(count => Date.now() - this.startTime)
);

cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文