RXJS如何进行多次点击上次事件?

发布于 2025-02-03 09:40:32 字数 269 浏览 2 评论 0原文

如何使用RXJ进行上次点击事件,以防止每个按钮点击的多个API调用? 我有一个机制,存在下一个按钮,该按钮会增加客户端##。 如果用户单击以正常速度单击下一个按钮,请接一个地获取下一个客户端插槽数据。但是,如果用户多次又一次地单击一个快速的单击,我不想仅仅显示终于打电话给API,因为它浪费了。由于用户无法遵循屏幕上的数据更改,因此我想根据最后一个递增的客户端##显示最新的点击触发数据。

使用debounctime用法,我可以实现它,但是没有debouncetime用法可以实现吗?

先感谢您。

How can I take the last click event with RxJS to prevent multiple API calls of each button click?
I have a mechanism where exists the next button which increments a clientSlot #.
If the user clicked on the next button with normal speed get the next client slot data one after another. But if the user clicked fast one after another multiple times I would not want to display only the at last because it wasted time calling the APIs. because the user is unable to follow the data changes on the screen, I would like to show the latest click triggered data based on the last incremented clientSlot #.

With debouncTime usage I able to implement it, but without debounceTime usage is it possible to implement?

Thank you in advance.

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

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

发布评论

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

评论(1

晚雾 2025-02-10 09:40:32

让使用 switchmap for -a> - 它将取消不必要的API请求

示例:

const yourHttpRequest$ = new Subject<InputApiData>();
const yourHttpResponse$: Observable<ClientData> = yourHttpRequest$.pipe(switchMap(
  inputApiData => this.httpService.getClientData(inputApiData)));
yourHttpResponse$.subscribe(clientData => { 
  // your logic to execute after API call was done
  });
yourHttpRequest$.next(inputApiData); // calling API

您有两个按钮,可以触发API调用。您可以将API调用放入点击事件处理程序中。

Let use switchMap for it - it will cancel unnecessary API requests

Example:

const yourHttpRequest$ = new Subject<InputApiData>();
const yourHttpResponse$: Observable<ClientData> = yourHttpRequest$.pipe(switchMap(
  inputApiData => this.httpService.getClientData(inputApiData)));
yourHttpResponse$.subscribe(clientData => { 
  // your logic to execute after API call was done
  });
yourHttpRequest$.next(inputApiData); // calling API

You have two buttons, which trigger API call. You can place API call in click event handler.

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