Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Javascript Ionic2/TypeScript循环完成后清除变量_Javascript_Typescript_Ionic Framework - Fatal编程技术网

Javascript Ionic2/TypeScript循环完成后清除变量

Javascript Ionic2/TypeScript循环完成后清除变量,javascript,typescript,ionic-framework,Javascript,Typescript,Ionic Framework,我试着做一个我认为很简单的例子。这是用Ionic 2和typescript编写的 我想循环2个hello数组。循环完成后,我想清除此消息。我有一个runHello()函数。我以为for循环会先运行,然后运行this.message=“”,但它会在循环完成之前运行 我在谷歌上搜索了很多关于这件事的信息,除了承诺之外找不到什么帮助。我认为这将是一个有点复杂的这和承诺似乎不工作,无论是离子或打字脚本 我有一个resetHello()函数,如果我不能让它工作,我会把它绑定到一个按钮上 对于学习编程来说是

我试着做一个我认为很简单的例子。这是用Ionic 2和typescript编写的

我想循环2个hello数组。循环完成后,我想清除此消息。我有一个runHello()函数。我以为for循环会先运行,然后运行this.message=“”,但它会在循环完成之前运行

我在谷歌上搜索了很多关于这件事的信息,除了承诺之外找不到什么帮助。我认为这将是一个有点复杂的这和承诺似乎不工作,无论是离子或打字脚本

我有一个resetHello()函数,如果我不能让它工作,我会把它绑定到一个按钮上

对于学习编程来说是相当新的。任何帮助都将不胜感激

export class Page1 {

  message: string = "";
  helloArr: Array<any>;

  constructor() {}

  sayHello(){
   setTimeout( ()=>{
      this.message ="Hello";
        }, 2000);
    };

  sayHello2(){
   setTimeout( ()=>{
      this.message ="Hello2";
    }, 3000);
  };

  runHello(){
    this.helloArr = [this.sayHello(), this.sayHello2()];

      for(let func of this.helloArr){
        func;
      };

      this.message = "this runs before for loop is done";

  }

  // resetHello(){
    //   this.message ="";
   // }

  }
导出类第1页{
消息:string=“”;
helloArr:数组;
构造函数(){}
你好{
设置超时(()=>{
this.message=“你好”;
}, 2000);
};
sayHello2(){
设置超时(()=>{
this.message=“Hello2”;
}, 3000);
};
runHello(){
this.helloArr=[this.sayHello(),this.sayHello2()];
为了(让func of this.helloArr){
func;
};
this.message=“这在for循环完成之前运行”;
}
//resetHello(){
//this.message=“”;
// }
}

setTimeout
调用是异步的,因此您必须等待它们的回调运行,然后再检查是否应重置此消息

    export class Page1 {

        message: string = "";
        helloArr: Array < any > ;
        count = 0;
        constructor() {}

        sayHello() {
            setTimeout(() => {
                this.message = "Hello";
                decrement()
            }, 2000);
        };

        sayHello2() {
            setTimeout(() => {
                this.message = "Hello2";
                decrement();
            }, 3000);
        };

        runHello() {
            this.helloArr = [this.sayHello(), this.sayHello2()];
            this.count = this.helloArr.length;
            for (let func of this.helloArr) {
                func;
            };

            // this.message = "this runs before for loop is done";

        }
        decrement() {
            this.count = --this.count;
            if (this.count == 0) {
                this.message = "";
            }
        }

    }
导出类第1页{
消息:string=“”;
helloArr:Array;
计数=0;
构造函数(){}
你好{
设置超时(()=>{
this.message=“你好”;
减量
}, 2000);
};
sayHello2(){
设置超时(()=>{
this.message=“Hello2”;
减量();
}, 3000);
};
runHello(){
this.helloArr=[this.sayHello(),this.sayHello2()];
this.count=this.helloArr.length;
为了(让func of this.helloArr){
func;
};
//this.message=“这在for循环完成之前运行”;
}
减量{
this.count=--this.count;
如果(this.count==0){
this.message=“”;
}
}
}