我得到了这段代码,每当服务器注册新的调用者时,它都会将调用者添加到数组中。
caller = new CallerData;
callers = new Array<CallerData>();
ngOnInit() {
this.callService.exposeCallerObservable().subscribe(
(x: CallerData) => {
this.caller = x;
this.callers.push(x);
},
(error: any) => {
console.warn("Could not subscribe to the Caller Observable!", error);
}
)
} 现在,我希望每个Caller在数组中只保留5-10秒,在此时间之后,它应该再次被删除。你能帮我吗?
发布于 2017-08-10 15:58:56
您可以使用js setTimeout函数
ngOnInit() {
this.callService.exposeCallerObservable().subscribe(
(x: CallerData) => {
this.caller = x;
this.callers.push(x);
setTimeout(()=>{
if(this.callers && this.callers.length > 0){
this.callers.shift();
}
},5000);
},
(error: any) => {
console.warn("Could not subscribe to the Caller Observable!", error);
});
} https://stackoverflow.com/questions/45607611
复制相似问题