【发布时间】:2019-08-11 20:04:54
【问题描述】:
我有一个 Observable 可以每 x 秒调用一次。
还有两个名为 Start 和 Stop 的按钮来控制 observable。
我想在用户按下 Stop 按钮时停止进程并取消订阅,并在按下 Start 后每 x 秒开始获取数据
到目前为止我有:
public subscription: Subscription;
public isLoading: boolean = false;
public isStopped: boolean = true;
// Start Getting Data
getData() {
this.isStopped = false;
this.isLoading = true;
this.subscription = this.proxy.InternetUserRwaits(model).pipe(
repeatWhen(completed => {
return completed.pipe(
tap(_ => this.isLoading = false),
delay(this.interval * 1000),
tap(_ => this.isLoading = this.isStopped ? false : true),
);
}),
)
.subscribe(
result => {
this.isLoading = false;
// ... rest of code
},
error => console.error(error)
);
}
// Stop Getting Data
stopGettingData() {
this.subscription.unsubscribe();
this.isStopped = true;
this.isLoading = false;
}
但在第一次停止后它不起作用
【问题讨论】:
-
从哪里获取数据?你的数据来源是什么?如果它是一个 observable,它会在什么时候发射,发射多少次?
-
是的,它是一个可观察的,每次都会发出整个数据。我想每隔 x 秒调用一次 observable