ionic 之 获取验证码

html代码

  <button ion-button clear [disabled]="disabled" (click)="getIdCode()">
    {{timeValue}}
  </button>

css代码

  button{
    color: white !important;
    background-color: orange!important;
  }

  button[disabled] {
    color: #222222 !important;
    opacity:0.5 !important;
    background-color: #eee!important;
  }

js代码

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';

@Component({
  selector: 'page-about',
  templateUrl: 'about.html'
})
export class AboutPage {

  private wait: number = 3;
  public countdown: number = this.wait;
  public timeValue = "获取验证码";
  public isReGetCode: boolean = false; // 是否是重新获取验证码
  public disabled: boolean = false; // 是否设置禁用

  private timer;

  constructor(public navCtrl: NavController) {

  }

  /**
   * 获取验证码
   */
  getIdCode() {

    this.isReGetCode = true;
    this.disabled = true;

    if (this.countdown == 0) {
      this.countdown = this.wait;
      if (!this.isReGetCode) {
        this.timeValue = "获取验证码";
      } else {
        this.timeValue = "重新获取";
      }
      this.disabled = false;

      if (this.timer){
        clearTimeout(this.timer);
      }
      return;
    } else {
      this.timeValue = "重新发送 (" + this.countdown + ")";
      this.countdown--;
    }
    //过1秒后执行倒计时函数
    this.timer = setTimeout(()=>this.getIdCode(),1000);
  }
}
获取验证码倒计时
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,687评论 1 92
  • 版权声明:本文为博主原创文章,未经博主允许不得转载。 webpack介绍和使用 一、webpack介绍 1、由来 ...
    it筱竹阅读 13,850评论 0 21
  • 无意中看到zhangwnag大佬分享的webpack教程感觉受益匪浅,特此分享以备自己日后查看,也希望更多的人看到...
    小小字符阅读 12,549评论 7 35
  • 1、立冬啥意思 “立”是建立、开始的意思;“冬”是“终也,万物收藏也”,意思是说秋季作物全部收晒完毕,收藏入库,动...
    爱码爱自由阅读 3,529评论 2 18
  • 东城文化广场,一文艺青年怀抱吉他,深情地演奏,旁边立一纸板,上书:为梦想走天涯,用灵魂去歌唱。……一名保安走过来将...
    红海早过了阅读 988评论 0 1