在 Angular 6 中使用订阅调用并等待间隔
call and wait in interval using subscribe in Angular 6
我每 10000 次调用一个方法。
我想让此函数 getAllNotificationsActed0()
每 10 秒调用一次,如果数据未在此间隔内出现,则不要再次调用该函数。如果在 10 秒内接收到数据,则调用该函数,如果在该函数的 10 秒内没有接收到数据,则不调用而是等待。
service.ts
public NotifGetAllActedNoActive(): Observable<Notifications[]> {
let headers = new Headers();
headers.append('x-access-token', this.auth.getCurrentUser().token);
return this.http.get(Api.getUrl(Api.URLS.NotifGetAllActedNoActive), {
headers: headers
})
.map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else {
return res.StatusDescription.map(notiff => {
return new Notifications(notiff);
});
}
});
}
component.ts
ngOnInit() {
this.subscription = Observable.interval(10000).subscribe(x => {
this.getAllNotificationsActed0();
});
}
getAllNotificationsActed0() {
this.notif.NotifGetAllActedNoActive().subscribe(notification0 => {
this.notification0 = notification0;
if (this.isSortedByDate) {
this.sortbydate();
}
});
}
有什么想法吗?
试试这个
您可以保留一个标志来查找等待的请求
//New Flag
requestWaiting : boolean = false;
public NotifGetAllActedNoActive(): Observable<Notifications[]> {
let headers = new Headers();
headers.append('x-access-token', this.auth.getCurrentUser().token);
return this.http.get(Api.getUrl(Api.URLS.NotifGetAllActedNoActive), {
headers: headers
})
.map((response: Response) => {
this.requestWaiting = false;
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else {
return res.StatusDescription.map(notiff => {
return new Notifications(notiff);
});
}
});
}
在区间内调用方法的地方使用flag
ngOnInit() {
this.subscription = Observable.interval(10000).subscribe(x => {
if(!this.requestWaiting){
this.requestWaiting = true;
this.getAllNotificationsActed0();
}
});
}
getAllNotificationsActed0() {
this.notif.NotifGetAllActedNoActive().subscribe(notification0 => {
this.notification0 = notification0;
if (!this.isSortedByDate) {
this.sortbydate();
}
});
}
已经触发的可观察对象将等待收到响应。
希望对你有所帮助
在你的组件中试试这个:
import { takeUntil } from 'rxjs/operators';
import { Subject, timer } from 'rxjs';
private _destroy$ = new Subject();
ngOnInit() {
this.getAllNotificationsActed0();
}
ngOnDestroy() {
this._destroy$.next();
}
getAllNotificationsActed0() {
this.notif.NotifGetAllActedNoActive()
.pipe(takeUntil(this._destroy$))
.subscribe(notification0 => {
this.notification0 = notification0;
if (this.isSortedByDate) {
this.sortbydate();
}
timer(10000).pipe(takeUntil(this._destroy$))
.subscribe(t => this.getAllNotificationsActed0() );
});
}
这是在组件销毁时停止管道的好方法。您可以使用 Subject 对象来实现这一点。您还可以停止任何必须在组件销毁时停止的管道。
我每 10000 次调用一个方法。
我想让此函数 getAllNotificationsActed0()
每 10 秒调用一次,如果数据未在此间隔内出现,则不要再次调用该函数。如果在 10 秒内接收到数据,则调用该函数,如果在该函数的 10 秒内没有接收到数据,则不调用而是等待。
service.ts
public NotifGetAllActedNoActive(): Observable<Notifications[]> {
let headers = new Headers();
headers.append('x-access-token', this.auth.getCurrentUser().token);
return this.http.get(Api.getUrl(Api.URLS.NotifGetAllActedNoActive), {
headers: headers
})
.map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else {
return res.StatusDescription.map(notiff => {
return new Notifications(notiff);
});
}
});
}
component.ts
ngOnInit() {
this.subscription = Observable.interval(10000).subscribe(x => {
this.getAllNotificationsActed0();
});
}
getAllNotificationsActed0() {
this.notif.NotifGetAllActedNoActive().subscribe(notification0 => {
this.notification0 = notification0;
if (this.isSortedByDate) {
this.sortbydate();
}
});
}
有什么想法吗?
试试这个
您可以保留一个标志来查找等待的请求
//New Flag
requestWaiting : boolean = false;
public NotifGetAllActedNoActive(): Observable<Notifications[]> {
let headers = new Headers();
headers.append('x-access-token', this.auth.getCurrentUser().token);
return this.http.get(Api.getUrl(Api.URLS.NotifGetAllActedNoActive), {
headers: headers
})
.map((response: Response) => {
this.requestWaiting = false;
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else {
return res.StatusDescription.map(notiff => {
return new Notifications(notiff);
});
}
});
}
在区间内调用方法的地方使用flag
ngOnInit() {
this.subscription = Observable.interval(10000).subscribe(x => {
if(!this.requestWaiting){
this.requestWaiting = true;
this.getAllNotificationsActed0();
}
});
}
getAllNotificationsActed0() {
this.notif.NotifGetAllActedNoActive().subscribe(notification0 => {
this.notification0 = notification0;
if (!this.isSortedByDate) {
this.sortbydate();
}
});
}
已经触发的可观察对象将等待收到响应。 希望对你有所帮助
在你的组件中试试这个:
import { takeUntil } from 'rxjs/operators';
import { Subject, timer } from 'rxjs';
private _destroy$ = new Subject();
ngOnInit() {
this.getAllNotificationsActed0();
}
ngOnDestroy() {
this._destroy$.next();
}
getAllNotificationsActed0() {
this.notif.NotifGetAllActedNoActive()
.pipe(takeUntil(this._destroy$))
.subscribe(notification0 => {
this.notification0 = notification0;
if (this.isSortedByDate) {
this.sortbydate();
}
timer(10000).pipe(takeUntil(this._destroy$))
.subscribe(t => this.getAllNotificationsActed0() );
});
}
这是在组件销毁时停止管道的好方法。您可以使用 Subject 对象来实现这一点。您还可以停止任何必须在组件销毁时停止的管道。