什么是等效于setTimeout()的Typescript / Angular 7(异步)

问题描述 投票:0回答:1

我想为我的聊天应用程序构建一个“打字指示器”。我正在尝试在用户停止按任意键5秒后使指示消失。

什么是Angular这样做的方式?

angular typescript settimeout
1个回答
1
投票

假设用户的输入在流中,请将其命名为userTypingStream$

userTypingStream$.pipe(
   (debounceTime(5000)
).subscribe(() => this.displayIndicator = false);

制作这样的流:

private userTypingStream$: Subject<string> = new Subject();
//...
onKeyPress = (event) => {
   this.userTypingStream$.next(event.target.value);
   //... and possibly the rest of the keypress handler here.
}

相关主题:可观察对象和主题(在开发Angular应用程序时几乎不可避免)。这些似乎是一个很好的起点:

https://angular.io/guide/observables

https://blog.angularindepth.com/rxjs-understanding-subjects-5c585188c3e1

© www.soinside.com 2019 - 2024. All rights reserved.