我试图在我的最后一个元素更新时停止间隔。
我不知道该怎么做。你能让我开始上轨道吗?
目前,当我离开页面时,我会清除间隔。
this.subscriptions.push(this.requestId.subscribe(id => {
this.store.dispatch(new requestActions.LoadRequest(id));
this.interval = setInterval(() => {
this.store.dispatch(new requestActions.LoadRequest(id));
}, 1700);
}));
我的效果:
@Effect()
loadRequest$: Observable<Action> = this.actions$
.ofType<requestActions.LoadRequest>(requestActions.LOAD_REQUEST)
.switchMap(action => {
return this.apiService
.loadRequest(action.payload)
.map((task: RequestTask[]) => new requestActions.LoadRequestSuccessAction(task))
.catch(error => of(new requestActions.LoadRequestFailureAction(error)));
});
我的减速机:
case request.LOAD_REQUEST_SUCCESS:
console.log("Payload : ", action.payload);
return {
...adapter.addAll(action.payload, state),
loaded: true,
selectedIndex: null
} as State;
答案 0 :(得分:0)
添加error
和complete
州。
this.subscriptions.push(this.requestId.subscribe(id => {
this.store.dispatch(new requestActions.LoadRequest(id));
this.interval = setInterval(() => {
this.store.dispatch(new requestActions.LoadRequest(id));
}, 1700);
}),(err)=>{console.log(err);},
()=>{
clearInterval(this.interval);
}
);
答案 1 :(得分:0)
我设法解决了我的问题:
export interface State extends EntityState<Event> {
request: Request }
我创建了一个函数
function getStatusOfEvents(payload : any){
const object = payload as Event[]
const events = object.map(obj => obj.status);
if (events.includes("PENDING")){
return true;
} else {
return false;
} }
我将值放在商店中:
export function reducer(state: State = initialState, action: request.Actions): State {
switch (action.type) {
case request.LOAD_EVENTS_SUCCESS:
console.log("Payload events : ", action.payload);
const status = getStatusOfEvents(action.payload)
return {
...adapter.addAll(action.payload, state),
refresh: status,
loaded: true,
} as State;
case request.LOAD_EVENTS:
case request.LOAD_EVENTS_FAILURE:
default:
return state;
}
}
最后我&#34;清楚&#34;返回值为false时的间隔
refresh: boolean;
this.subscriptions.push(this.requestId.subscribe(id => {
this.store.dispatch(new requestActions.LoadEvents(id));
this.interval = setInterval(() => {
this.store.dispatch(new requestActions.LoadEvents(id));
this.store.select(fromRoot.getRefreshEntitiesState).subscribe(data => this.refresh = data)
console.log("Refresh :", this.refresh)
if (this.refresh != undefined && this.refresh === false) {
clearInterval(this.interval);
}
}, 1700);
}))