首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 2,throttleTime on subject on observable from subject

Angular 2,throttleTime on subject on observable from subject
EN

Stack Overflow用户
提问于 2019-11-04 23:29:58
回答 1查看 335关注 0票数 2

我正在尝试让throttleTime生效,但由于某种原因,它没有生效。我有以下几点:

代码语言:javascript
复制
// Class Properties    
private calendarPeriodSubject: Subject<x> = new Subject<x>();
private calendarPeriodObservable$ = this.calendarPeriodSubject.asObservable();


// Throttling fails here (Inside constructor):
const calendarPeriodSubscription = this.calendarPeriodObservable$.pipe(throttleTime(750)).subscribe(async (calendar: x) => {
    // Do http stuff here
  }
});

主题的名称是这样的:

代码语言:javascript
复制
this.calendarPeriodSubject.next(x);

我还尝试了:

代码语言:javascript
复制
this.calendarPeriodSubject.pipe(throttleTime(1000)).subscribe({next: (x) => x});

我想处理第一次,下面的点击应该不会有任何影响后,ieg 750ms -以防止服务器得到基本上垃圾邮件。

有谁知道吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2019-11-05 21:44:50

问题是你在你的用例中使用了错误的操作符。我理解你的解释,你想通过你的第一个呼叫发送,并停止任何进一步的呼叫到您的服务器一定数量的毫秒。但throttleTime(sec)所做的只是在操作上设置一个计时器,并在稍后sec ms执行它。因此,您的服务器仍然会被垃圾邮件,只是几毫秒之后。

你的案子向我发出了debounceTime()的尖叫。debounceTime docu

这将禁止在发出值后的指定时间内通过可观察对象传递任何进一步的数据。

因此,如果您使用以下内容,您的代码应该没有问题:

代码语言:javascript
复制
const calendarPeriodSubscription = 
this.calendarPeriodObservable$.pipe(debounceTime(750)).subscribe((calendar: x) => {
    // Stuff with returned data
});    
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58696494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档