rev*_*veN 0 arrays typescript angular
我得到了此代码,每当服务器注册新的调用者时,该代码就会将调用者添加到数组中。
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);
}
)
}
Run Code Online (Sandbox Code Playgroud)
现在,我想将每个调用方在数组中仅保留5-10秒,此后应将其再次删除。你能帮我吗?
您可以使用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);
});
}
Run Code Online (Sandbox Code Playgroud)