收藏
回答

clearInterval不起作用? 60秒倒计时到<= -1 清除计时器

https://developers.weixin.qq.com/s/paWbsomw7hAp本来应该到0就不会再减了 一直减0 -1 -2 持续下去

最后一次编辑于  2022-06-30
回答关注问题邀请回答
收藏

1 个回答

  • 小勇童学🍊
    小勇童学🍊
    2022-06-30
      isDisabled: false,
      countDownNum: 60// 倒计时初始值
      timer: ""
    
    
    var countDownNum = _this.data.countDownNum // 获取倒计时初始值
          _this.data.timer = setInterval(function () {
            countDownNum -= 1
            _this.setData({
              countDownNum: countDownNum,
              isDisabled: true
            })
            if (countDownNum <= -1) {
              clearInterval(_this.data.timer)
              // 取消置顶的setInterval函数将要执行的代码
              _this.setData({
                countDownNum: 60,
                isDisabled: false
              })
            }
          }, 1000)
    

     <block wx:if="{{countDownNum == 60 || countDownNum == -1}}">

          <van-button bindtap="getCode" slot="button" size="small" style="margin-top: 15rpx;display: inline-block;margin-left: 30rpx;" custom-style="border-radius: 14rpx;width:200rpx;height:60rpx;margin-bottom:20rpx;">

            获取验证码

          </van-button>

        </block>

        <!-- 60s之后可重新获取验证码 -->

        <block wx:else>

          <van-button disabled='{{isDisabled}}' style="margin-top: 15rpx;display: inline-block;margin-left: 30rpx;" custom-style="border-radius: 14rpx;width:200rpx;height:60rpx;margin-bottom:20rpx;" slot="button" size="small">

            <view class='yanzen-row'>

              <view class='time'>{{countDownNum}}</view>

              <view class="smiao">s后重新发送</view>

            </view>

          </van-button>

        </block>


    2022-06-30
    有用
    回复
登录 后发表内容