简体   繁体   English

如何在 AngularJS 中超时或延迟工作

[英]How to get timeout or delay working in AngularJS

I'm a complete noob when it comes to AngularJS.谈到 AngularJS,我完全是个菜鸟。 I need some help getting a message to show for a period of time and then go away.我需要一些帮助来获取消息显示一段时间然后离开。

The following code writes a judge's score to a database and then prevents that judge from scoring again until the score has been cleared from the database.以下代码将裁判的分数写入数据库,然后阻止该裁判再次评分,直到该分数从数据库中清除。 I want a message to appear below the judge panel when the judge submits their score, something like "OK!".我希望在裁判提交分数时在裁判小组下方显示一条消息,例如“OK!”。 Currently, it shows up when isLoading = true is set but only very briefly because as you can see from the code below it gets turned to false.目前,它在设置 isLoading = true 时显示,但只是非常短暂,因为正如您从下面的代码中看到的那样,它会变为 false。 I need it to turn to false automatically, but only after 3 or so seconds.我需要它自动变为 false,但仅在 3 秒左右之后。

async send(): Promise<any> {
try {
  this.isLoading = true;
  const star: { isValid: boolean } = await this.judgeService.submitScoreIsValid(this.id).toPromise();
  if (star.isValid === true) {
    await this.judgeService.write(this.id, {score: this.currentScore}).toPromise();
    this.status = true;
  } else {
    this.status = false;
  }
} catch (e) {
  console.log(e);
} finally {
  this.isLoading = false;
}
}

I want the isLoading to equal true but only for about 3 seconds.我希望 isLoading 等于 true 但只有大约 3 秒。 How can I do this?我怎样才能做到这一点?

I've tried going:我试过:

this.isLoading=true;    
$timeout(function(){ this.isLoading=false; }, 3000);

but it didn't work, it just stays up and doesn't go away after 3sec.但它没有用,它只是保持状态并且在 3 秒后不会消失。 Can anyone help?任何人都可以帮忙吗?

My frontend is this:我的前端是这样的:

<div *ngIf="isLoading">
     <h5>OK!</h5>
</div>

*edited to reflect changes made by the suggestion of one of the answers below. *编辑以反映以下答案之一的建议所做的更改。

First of all, if you have the choice and aren't stuck with updating legacy code, life will be much easier if you work with the new Angular, instead of the old AngularJS.首先,如果您有选择并且不坚持更新遗留代码,那么如果您使用新的 Angular 而不是旧的 AngularJS,生活会容易得多。 You won't have to worry so much about the digest cycle , and whether or not changes you make to the values of variables are detected by AngularJS.您不必太担心摘要循环,以及您对变量值所做的更改是否会被 AngularJS 检测到。

At any rate, the code you've written tells angular to set isLoading to true after 3 seconds, not for 3 seconds.无论如何,你写的代码告诉角度来设定isLoadingtrue3秒,而不是3秒。 What you need to do is:你需要做的是:

$timeout(function() { this.isLoading = true; });
$timeout(function() { this.isLoading = false; }, 3000);

...that is, first set it to true , then after 3 seconds, set it to false . ...也就是说,首先将其设置为true ,然后在 3 秒后将其设置为false

you need to do this:你需要这样做:

$timeout(() => this.isLoading = false, 3000);

non arrow functions create a new this context, arrow functions will preserve the outer this非箭头函数创建一个新的this上下文,箭头函数将保留外部this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM