angular - 取消订阅条件不允许在 Angular RXJS 中订阅
问题描述
我有一项显示穿着、信息和错误通知的服务。有时通知来得很频繁。让我们同时说 20-30,我试图通过在特定时间间隔发生时取消订阅来避免这件事。
取消订阅后,不再订阅。
const locationsSubscription = this.messageService
.getMessage()
.pipe(timeInterval(), tap(console.log))
.subscribe(message => {
if (message.interval < 500) {
locationsSubscription.unsubscribe();
}
else {
switch (message.type){
case MessageType.Info:
this.showInfo(message.text);
break;
case MessageType.Warning:
this.showWarning(message..text);
break;
case MessageType.Error:
this.showError(message.value);
break;
}
}
});
解决方案
只需使用debounceTime
或throttleTime
在经过一段时间后触发警告:
this.messageService
.getMessage()
.pipe(
debounceTime(500) // to allow messages to trigger after 500ms of inactivity (idle)
// throttleTime(500) // to allow only one message per 500ms (first or last)
)
推荐阅读
- html - 如何在视差网站中添加正常背景
- bash - 如何使用 bash 命令在 xml 中的匹配模式后附加新标签
- r - ggplot:添加颜色美学更改堆栈顺序
- json - 在 ColdFusion 中将数据转换为 JSON 时出错
- html - Internet Explorer / Edge 不发送带有 URL 的片段
- angularjs - AngularJS - ng-repeat 多维数组
- bash - 根据另一列的内容在 CSV 中创建一个新列
- angular - Angular Material 6 - 如何以编程方式设置 mat-paginator 偏移量?
- hazelcast - Hazelcast:如何获得近缓存的地图条目命中计数?
- c# - Linq 没有加载整个对象