angular - 带去抖动的 BehaviorSubject 订阅
问题描述
我有 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
推荐阅读
- xamarin - Xamarin.Android 应用程序关闭后处理的后台任务
- javascript - 在 JavaScript 中一次调用打印 JSON 路径和变量结果
- python - 计算具有相同列和一些不匹配行的两个熊猫数据框之间的差异
- c - 《Head First C》一书demo中关于strstr()和fgets()的问题
- amazon-web-services - boto3 filter_log_events 不返回 cloudwatch 日志
- java - 如何通过使用 testng 设置优先级在多个类中一个一个地运行所有方法
- php - 钩子 pre_controller 中的 Codeigniter 访问会话
- php - MySQL Workbench 与 PHP 的性能不同
- javascript - React.createElement:类型无效(地图)
- python - 仅 50% 的 RandomForestClassifier 用于好坏电影评论的情感分析