每秒收到API的更新
Angular RXJS代码进行API调用,以从API服务获取信息 但是,我需要每秒提出请求。我了解我需要使用可观察到的东西,但不确定如何进行。
An Angular RxJS code makes an API call to get information from an API service
However, I need the request to be made every second. I understand that I need to use Observables but not sure how to proceed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用rxjs Interval 投票操作员。
You can use rxjs interval operator for polling.
此方法应效果很好(在伪代码中)
注意:
date> dyailmap
如果Interval
事件,则如果forkjoin
花费超过1s,则它将每1秒继续This approach should work well (in pseudo code)
Note: the
exhaustMap
will ignore theinterval
event if theforkJoin
takes longer than 1s, and then it will continue every 1s您可以将
Intervel()
使用每个x
毫秒。您可以在pipe()
dyale> dyale> dyale> dyalemmap()中执行forkjoin()
。订阅类似的组件可观察到:
注意:您也可以使用一个数组,此示例只使用了两个不同的属性。
组件:
这是一个有效的示例
注意:请参阅 intercept服务查看虚拟网络API的工作方式。如果存在实际的API,则不需要。
You can use
interval()
to ping the server everyx
milliseconds. You can the do theforkJoin()
in thepipe()
within anexhaustMap()
.Subscribe to the component's Observable like this:
Note: You could have used an array too, this example just uses two different properties.
The component:
Here is a working example
Note: See the intercept service to see how the dummy network api works. This isn't needed if an actual API exists.