循环完成后,Ionic2/TypeScript清除变量
Ionic2/TypeScript clear variable after loop is complete
我正在尝试做我认为应该是一个非常简单的例子。这是用Ionic 2用打字本写的。
我想循环遍历2个hello数组。循环完成后,我想清除这个.message。我有一个runHello()函数。我原以为for循环会先运行,然后运行this.message=",但它在循环完成之前运行。
我在谷歌上搜索了很多关于这件事的信息,除了Promises之外,找不到其他任何帮助。我认为这会有点复杂,而且承诺似乎不适用于Ionic或Typescript。
我有一个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 ="";
// }
}
setTimeout
调用是异步的,因此在检查是否应重置this.message
之前,必须等待它们的回调运行。
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 = "";
}
}
}
相关文章:
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 单击jquery清除输入值
- Foreach无法在Typescript中工作
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 为什么无法在TypeScript中导出类实例
- 如何将TypeScript对象转换为普通对象
- Angular 2.0 with JavaScript or TypeScript?
- "实例范围”;TypeScript类的getter/setter
- 如何在大型nodeJS代码的基础上逐步引入typescript
- 如何在Microsoft VirtualEarth 6.3中使用纯javascript清除整个形状层
- 清除以前的$_GET值或不获取仅隐藏字段的值
- 从HTML调用typescript文件中的函数
- 如何将js库与typescript一起使用
- 理解typescript中的构造函数接口
- 在gump和nodejs中使用Typescript时,未定义对require和exports的引用
- Javascript清除缓存以清除基本身份验证凭据
- Typescript导入导出的类会发出require(..),从而导致浏览器错误
- 为其他JS生成定义“;编译器”;来自TypeScript定义文件
- Typescript angularjs$http获取响应类型(避免使用<any>)
- 循环完成后,Ionic2/TypeScript清除变量