Javascript 角度2,受试者观察到的节流时间

Javascript 角度2,受试者观察到的节流时间,javascript,angular,rxjs,observable,Javascript,Angular,Rxjs,Observable,我试图使throttleTime生效,但由于某些原因,它没有生效。我有以下资料: // Class Properties private calendarPeriodSubject: Subject<x> = new Subject<x>(); private calendarPeriodObservable$ = this.calendarPeriodSubject.asObservable(); // Throttling fails here (Insi

我试图使throttleTime生效,但由于某些原因,它没有生效。我有以下资料:

// 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
  }
});

我还尝试了:

this.calendarPeriodSubject.pipe(throttleTime(1000)).subscribe({next: (x) => x});
我想第一次处理,在ieg 750ms之前和之后,下面的点击不应该有任何影响-基本上防止服务器收到垃圾邮件

有人知道吗


谢谢

问题在于您对用例使用了错误的运算符。我理解您的解释,您希望通过第一次调用发送,并停止对服务器的任何进一步调用一段时间。但是
throttleTime(sec)
所做的只是在操作上放置一个计时器,然后在
sec
ms后执行它。所以你的服务器仍然会被垃圾邮件,仅仅几毫秒后

你的案子为我尖叫

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

因此,如果您使用以下内容,您的代码应该很好:

const calendarPeriodSubscription = 
this.calendarPeriodObservable$.pipe(debounceTime(750)).subscribe((calendar: x) => {
    // Stuff with returned data
});    

如果你能在stack blitz中重新创建并隔离问题,我很乐意提供帮助。在此之前,我从rxjs中找到了一些文档:这不会阻止服务器被垃圾邮件攻击,它只会“延迟”它感谢@c_ogo在我之前的评论中指出这一点(我的+1:)
const calendarPeriodSubscription = 
this.calendarPeriodObservable$.pipe(debounceTime(750)).subscribe((calendar: x) => {
    // Stuff with returned data
});