怎么倒计时验证码?

小程序 文章 2020-09-09 13:21 980 0 全屏看文

AI助手支持GPT4.0

怎么倒计时验证码?How to count down the verification code?

怎么倒计时验证码?
Page({  data: {      smstxt:'获取'  },sendonesms:function(){    var that = this;    if( wait == 0){      this.setData({        smstxt:'获取'      })      wait = 60;    }else{      this.setData({        smstxt:wait.toString()      })     wait--;      setTimeout(function() {        that.sendonesms      }, 1000)    }  })} 点击之后,就变成60,就不倒计时了,怎么回事啊。哪里写错了,感觉没错啊。


<label  bindtap="sendonesms">{{smstxt}} label>


Page ({data: {smstxt: 'get'}, s endonesms:function (){    var that = this;    if( wait == 0){       this.setData ({smstxt: 'get'}) wait = 60;} else { this.setData ({         smstxt:wait.toString ()      })     wait--;       setTimeout(function() {         that.sendonesms After you click it, it turns to 60, and there is no countdown. What's going on. What's wrong? I feel right.

回答:

?:

点击获取验证码的函数

let time = 60;

let timer = setInterval(() => {

      time--;

      if (time > 0{

        that.setData({

          codeTxt: time + 's',        

   IsDisAbled: false,//不可点击

        })

      } else {

        that.setData({

          codeTxt: '再次获取验证码',

          IsDisAbled: true//可点击

        })

        clearInterval(timer);

      }

    }, 1000);


少年:谢谢,我找到了,别的做法,我就想知道,我这个错在哪里了。
?:你把你这个所有的都贴出来 我看看。你wait定义的什么。
少年:wait是全局变量,在最上面,pages,上面。let wait = 60;
少年:

wait是全局变量,在最上面

-EOF-

AI助手支持GPT4.0