Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Angular/Typescript中,在几秒钟后移除/拼接数组的元素_Arrays_Angular_Typescript - Fatal编程技术网

Arrays 在Angular/Typescript中,在几秒钟后移除/拼接数组的元素

Arrays 在Angular/Typescript中,在几秒钟后移除/拼接数组的元素,arrays,angular,typescript,Arrays,Angular,Typescript,我得到了这段代码,每当服务器注册新的调用者时,就会将调用者添加到数组中 caller = new CallerData; callers = new Array<CallerData>(); ngOnInit() { this.callService.exposeCallerObservable().subscribe( (x: CallerData) => { this.call

我得到了这段代码,每当服务器注册新的调用者时,就会将调用者添加到数组中

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=新的被调用数据;
调用者=新数组();
恩戈尼尼特(){
此.callService.ExposeCallerRobservable().subscribe(
(x:CallerData)=>{
this.caller=x;
这是一个.callers.push(x);
},
(错误:any)=>{
console.warn(“无法订阅可观察的调用方!”,错误);
}
)
}  

现在我想让每个调用者在数组中只保留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);
         });
}  

您可以使用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);
         });
}  

this.callers.push(x)之后
您可以编写类似于
setTimeout(()=>{this.callers=[];},5000)
完美!谢谢,这就是我想要的:)我将提供它作为答案:-)这将在5秒后清除整个阵列,对吗?我必须如何修改它,以便它跟踪每个新调用方被推入数组的5秒,并“一个接一个”地删除它们?在
this.callers.push(x)之后
您可以编写类似于
setTimeout(()=>{this.callers=[];},5000)
完美!谢谢,这就是我想要的:)我将提供它作为答案:-)这将在5秒后清除整个阵列,对吗?我必须如何修改它,以便它跟踪每个新调用方的5秒钟时间,这些新调用方被推入阵列并“一个接一个”地删除它们?这将一次删除所有调用方。我不确定这是OP想要的。我的意思是,如果一个呼叫者每秒钟都有一个呼叫者?然后,第五个将被删除immediately@TamasHegedus由于OP评论“完美!谢谢,这就是我想要的:)”我假设这就是OP想要的:-)但当然拼接可以用来移除特定元素。塔马斯实际上是对的:D我评论了问题^^@echonax trust noone.:不,我不明白你的意思,我会在塔马谢格杜斯给出完美答案之前设法解决它;D我现在正在工作,因此编写代码需要一段时间。这将立即删除所有调用方。我不确定这是OP想要的。我的意思是,如果一个呼叫者每秒钟都有一个呼叫者?然后,第五个将被删除immediately@TamasHegedus由于OP评论“完美!谢谢,这就是我想要的:)”我假设这就是OP想要的:-)但当然拼接可以用来移除特定元素。塔马斯实际上是对的:D我评论了问题^^@echonax trust noone.:不,我不明白你的意思,我会在塔马谢格杜斯给出完美答案之前设法解决它;我现在在工作,所以写代码需要一段时间。