我有BehaviorSubject属性
public results$ = new BehaviorSubject(null);
说我订阅了此书
this.results$.subscribe(() => this.find());
每当我在输入元素内部的键盘上按任意键时,就会调用订阅。因此,为防止向服务器发送许多请求,我添加了debounceTime()
this.results$
.pipe(debounceTime(500))
.subscribe(() => this.find());
但是我需要立即调用第一个this.find()
而不会产生任何反跳。
我该怎么做?
答案 0 :(得分:1)
我建议您使用throttleTime
代替debounceTime。它传播第一个值,然后等待指定的时间。
也将BehaviorSubject
更改为Subject
。
公开结果$ =新的Subject();
this.results$
.pipe(
filter(value => !!value && value.length >= 3),
throttleTime(500))
.subscribe(() => this.find());
请在此处了解有关油门时间,审核时间和去抖时间的信息-https://rxjs-dev.firebaseapp.com/guide/operators