gpt4 book ai didi

angular - 带有去抖动的 BehaviorSubject 订阅

转载 作者:行者123 更新时间:2023-12-05 02:51:03 25 4
gpt4 key购买 nike

我有 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

公开结果$ = 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

关于angular - 带有去抖动的 BehaviorSubject 订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63435647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com