【发布时间】:2020-03-14 04:50:59
【问题描述】:
我有一个输入元素,我想在其中显示一个自动完成解决方案。我尝试使用 RxJS 控制 HTTP 请求的数量。我想做的是:HTTP 请求仅在用户停止输入 1 秒后开始。
我有这个代码:
of(this.inputBox.nativeElement.value)
.pipe(
take(1),
map((e: any) => this.inputBox.nativeElement.value),
// wait 1 second to start
debounceTime(1000),
// if value is the same, ignore
distinctUntilChanged(),
// start connection
switchMap(term => this.autocompleteService.search({
term: this.inputBox.nativeElement.value
})),
).subscribe((result: AutocompleteResult[]) => {
console.log(result);
});
问题是debounceTime(1000) 没有等待继续管道。 switchMap 在每个 keyup 事件之后立即启动。
如何在用户完成输入后等待 1 秒?
【问题讨论】: