当标志变为真时开始轮询
Start polling when flag becomes true
我想在atrribute的值变为真时开始轮询,有没有什么方法可以在flag变为真时开始轮询,目前我的实现是:
ngOnInit(){
//want to start when startedPoll becomes true otherwise dont
let url = 'getStatus'
this.pollingSubscribe = interval(5000).pipe(
switchMap(() => this.http.getCall(url))
).subscribe(data => {
if(data['success']) {
this.pollingData =data['result']
this.processData(this.pollingData)
}
}, error =>{
this.error = true;
this.errorData = this.createPollingFiles;
})
}
startPoll(){
this.startedPoll = true;
}
当startedPoll变为true时开始轮询数据,请不要提示if 和 else 实施
如果 startedPoll
变成 true
的唯一方法是调用 startPoll
方法,那么只需将 subscribe
调用从 ngOnInit
方法进入 startPoll
:
ngOnInit() {
// want to start when startedPoll becomes true otherwise dont
let url = 'getStatus'
this.pollingObservable = interval(5000).pipe(
switchMap(() => this.http.getCall(url))
);
}
startPoll() {
this.pollingSubscribe = this.pollingObservable.subscribe(data => {
if (data['success']) {
this.pollingData = data['result']
this.processData(this.pollingData)
}
}, error => {
this.error = true;
this.errorData = this.createPollingFiles;
})
this.startedPoll = true;
}
ngOnDestroy() {
if (this.pollingSubscribe) {
this.pollingSubscribe.unsubscribe();
}
}
确保在轮询活动时 startPoll
不会被调用超过一次。 否则,Subscription
已经存储在 this.pollingSubscribe
会丢失,轮询频率会翻倍,部分轮询会在组件销毁后继续。
我想在atrribute的值变为真时开始轮询,有没有什么方法可以在flag变为真时开始轮询,目前我的实现是:
ngOnInit(){
//want to start when startedPoll becomes true otherwise dont
let url = 'getStatus'
this.pollingSubscribe = interval(5000).pipe(
switchMap(() => this.http.getCall(url))
).subscribe(data => {
if(data['success']) {
this.pollingData =data['result']
this.processData(this.pollingData)
}
}, error =>{
this.error = true;
this.errorData = this.createPollingFiles;
})
}
startPoll(){
this.startedPoll = true;
}
当startedPoll变为true时开始轮询数据,请不要提示if 和 else 实施
如果 startedPoll
变成 true
的唯一方法是调用 startPoll
方法,那么只需将 subscribe
调用从 ngOnInit
方法进入 startPoll
:
ngOnInit() {
// want to start when startedPoll becomes true otherwise dont
let url = 'getStatus'
this.pollingObservable = interval(5000).pipe(
switchMap(() => this.http.getCall(url))
);
}
startPoll() {
this.pollingSubscribe = this.pollingObservable.subscribe(data => {
if (data['success']) {
this.pollingData = data['result']
this.processData(this.pollingData)
}
}, error => {
this.error = true;
this.errorData = this.createPollingFiles;
})
this.startedPoll = true;
}
ngOnDestroy() {
if (this.pollingSubscribe) {
this.pollingSubscribe.unsubscribe();
}
}
确保在轮询活动时 startPoll
不会被调用超过一次。 否则,Subscription
已经存储在 this.pollingSubscribe
会丢失,轮询频率会翻倍,部分轮询会在组件销毁后继续。