【问题标题】:how to create count down timer on button click in angular如何在按钮单击时创建倒数计时器
【发布时间】:2020-07-29 02:27:53
【问题描述】:

您好,我想在 Angular 中忘记密码屏幕

sinario

  1. 用户输入他们的电子邮件 ID 并点击提交按钮。
  2. 单击提交按钮后,该按钮应被禁用并显示 2 分钟的倒计时我已附上屏幕截图
  3. 2 分钟后恢复正常

before click 和点击后after click

忘记密码.component.html

<form class="form-horizontal login100-form m-auto" [formGroup]="loginForm">
          <p class="h4 login-header">Forgot password</p>
          <div class="form-group form-group-lg">
            <mat-form-field [appearance]="'outline'" class="w-100">
              <mat-label>Email-id</mat-label>
              <input matInput formControlName="user_email" #t>
            </mat-form-field>
          </div>
          <div class="form-group form-group-lg">
            <button type="button" class="btn login-button btn-block"
                    (click)="onResetPassword()" [disabled]="f.user_email.errors && f.user_email.errors.required">
              Send password reset
              link
            </button>
          </div>
          <div class="contact100-form-checkbox ">
            <div class="d-flex">
              <div class="mr-2">
                <a (click)="forgetpassword = false" class="txt1"
                   style="   font-size: 20px;">Sign-in</a>
              </div>
            </div>
          </div>
        </form>

忘记密码.component.ts

onResetPassword() {
const email = this.loginForm.value.user_email;

console.log(this.loginForm.controls.user_email.errors);
this.userService.gerResetPassword(email).subscribe(data => {
    alert(JSON.stringify(data));
    console.log(data);
    // this.show = true;
  },
  error => {
    console.log(error);
  }
);

}

我找到了一些在线资源,但没有任何帮助...

【问题讨论】:

    标签: html css angular typescript validation


    【解决方案1】:

    https://stackblitz.com/edit/angular-kswlfw

    你可以使用 rxjs 库中的 Observable。

    创建间隔

    this.timer = interval(1000)
          .pipe(
            takeUntil(this.ispause)
          );
    
        this.timerObserver = {
    
          next: (_: number) => {  
             if(this.time==0){this.ispause.next;}
              this.time -= 1;        
          }
        };
    

    并在点击函数中调用它

    <button (click)="goOn()"> {{secondsToHms(time)}}</button>
    goOn() {
    
        this.timer.subscribe(this.timerObserver);
      }
    

    这个函数是用来格式化显示计时器

    secondsToHms(d) {
        d = Number(d);
        var m = Math.floor(d % 3600 / 60);
        var s = Math.floor(d % 3600 % 60);
    
        var mDisplay = m > 0 ? m + (m == 1 ? ": " : " : ") : "00";
        var sDisplay = s > 0 ? s + (s == 1 ? "" : "") : "00";
        return mDisplay + sDisplay; 
    }
    

    【讨论】:

    • 您好如何根据点击或动态更改按钮文本
    • 如果你将变量连接到间隔,那么当间隔继续时,你的文本也会改变,因为你在 html 中绑定文本。它总是在后端监听时间
    • 我是 angular 兄弟的新手,你能举个例子给我解释一下吗...
    • Angular 不像 javascript。在 angular html 和 typescript 中有两种工作方式。这意味着如果您在 typescript 中更改了某些内容,那么在 html 中它会自动更改。如果您在 html 中更改某些内容,它会自动在 typescript 中更改。在这个例子中,我们用定时器每秒改变时间变量,当变量改变时,html也会自动改变
    • 你现在明白了吗?我们只是在这里随着时间间隔改变变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多