带有去抖动的 BehaviorSubject 订阅
BehaviorSubject subscription with debounce
我有 BehaviorSubject 属性
public results$ = new BehaviorSubject(null);
假设我订阅了这个
this.results$.subscribe(() => this.find());
每次我在输入元素内按下键盘上的任意键时都会调用订阅。所以,为了防止对服务器的许多请求,我添加了 debounceTime()
this.results$
.pipe(debounceTime(500))
.subscribe(() => this.find());
但我需要立即调用第一个 this.find()
,没有任何去抖动。
我该怎么做?
我建议您使用 throttleTime
而不是 debounceTime。它传播第一个值,然后等待指定的时间量。
同时将 BehaviorSubject
更改为 Subject
。
public 结果$ = new Subject();
this.results$
.pipe(
filter(value => !!value && value.length >= 3),
throttleTime(500))
.subscribe(() => this.find());
请在此处阅读有关 throttleTime、auditTime 和 debounceTime 的信息 - https://rxjs-dev.firebaseapp.com/guide/operators
我有 BehaviorSubject 属性
public results$ = new BehaviorSubject(null);
假设我订阅了这个
this.results$.subscribe(() => this.find());
每次我在输入元素内按下键盘上的任意键时都会调用订阅。所以,为了防止对服务器的许多请求,我添加了 debounceTime()
this.results$
.pipe(debounceTime(500))
.subscribe(() => this.find());
但我需要立即调用第一个 this.find()
,没有任何去抖动。
我该怎么做?
我建议您使用 throttleTime
而不是 debounceTime。它传播第一个值,然后等待指定的时间量。
同时将 BehaviorSubject
更改为 Subject
。
public 结果$ = new Subject();
this.results$
.pipe(
filter(value => !!value && value.length >= 3),
throttleTime(500))
.subscribe(() => this.find());
请在此处阅读有关 throttleTime、auditTime 和 debounceTime 的信息 - https://rxjs-dev.firebaseapp.com/guide/operators