我正在尝试让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.next(x);我还尝试了:
this.calendarPeriodSubject.pipe(throttleTime(1000)).subscribe({next: (x) => x});我想处理第一次,下面的点击应该不会有任何影响后,ieg 750ms -以防止服务器得到基本上垃圾邮件。
有谁知道吗?
谢谢!
发布于 2019-11-05 21:44:50
问题是你在你的用例中使用了错误的操作符。我理解你的解释,你想通过你的第一个呼叫发送,并停止任何进一步的呼叫到您的服务器一定数量的毫秒。但throttleTime(sec)所做的只是在操作上设置一个计时器,并在稍后sec ms执行它。因此,您的服务器仍然会被垃圾邮件,只是几毫秒之后。
你的案子向我发出了debounceTime()的尖叫。debounceTime docu
这将禁止在发出值后的指定时间内通过可观察对象传递任何进一步的数据。
因此,如果您使用以下内容,您的代码应该没有问题:
const calendarPeriodSubscription =
this.calendarPeriodObservable$.pipe(debounceTime(750)).subscribe((calendar: x) => {
// Stuff with returned data
}); https://stackoverflow.com/questions/58696494
复制相似问题