zoukankan      html  css  js  c++  java
  • 小程序中按钮验证码倒计时

    wxml文件:

    <view>
      <button type="primary" bindtap="countdown" disabled="{{isDisabled}}">{{title}}</button>
    </view>
    <view>
      <button type="primary" bindtap="countdown1" disabled="{{isDisabled}}">{{title}}</button>
    </view>
    

    js文件

      data: {
        title:"发送验证码", // 按钮中显示的字符
        titleConst:"发送验证码", // 重置后的按钮中显示的字符
        count:3,   // 倒计时的秒数
        countConst:3, // 重置后的倒计时的秒数
        isDisabled:false // 按钮是否禁用
      },  
    
      // setInterval中用箭头函数,保证this和外部一致
      countdown: function(){
        let count = this.data.count;
        // 当count不为0开始倒计时,当count为0就关闭倒计时
        // 设置定时器
        var countdown = setInterval(() => {
          if( count == 0 ){
            this.setData({
              title:this.data.titleConst,
              count: this.data.countConst,
              isDisabled:false
            });
            // 取消定时器
            clearInterval(countdown);
          } else {
            this.setData({
              title:count-- + "s后重试",
              isDisabled:true
            });
          }
        }, 1000);
      },
      // 用that保存this,防止在setInterval中this被替换
      countdown1: function(){
        let that = this;
        let count = this.data.count;
        // 当count不为0开始倒计时,当count为0就关闭倒计时
        // 设置定时器
        var countdown = setInterval(function(){
          if( count == 0 ){
            that.setData({
              title:that.data.titleConst,
              count: that.data.countConst,
              isDisabled:false
            });
            // 取消定时器
            clearInterval(countdown);
          } else {
            that.setData({
              title:count-- + "s后重试",
              isDisabled:true
            });
          }
        }, 1000);
      }
    
  • 相关阅读:
    event loop笔记
    webpack不同devtools打包对比
    windows 7系统搭建本地SVN服务器的过程
    Eclipse安装SVN插件
    总结eclipse中安装maven插件
    myeclipse building workspace如何禁止及提高myeclipse速度
    基于SAML的单点登录介绍
    使用 CAS 在 Tomcat 中实现单点登录
    SSO之CAS备忘
    Maven环境变量配置
  • 原文地址:https://www.cnblogs.com/to-red/p/12700231.html
Copyright © 2011-2022 走看看