위챗 애플릿 문자 발송 카운트다운 코드

1592 단어
test.wxml


test.js
Page({
  data: {
    send_text: '  ',
    send_disabled: false,
    send_time: null
  },
  onLoad: function (options) {
  },
  onReady: function () {
    //       
  },
  onShow: function () {
    //     
  },
  onHide: function () {
    //     
  },
  onUnload: function () {
    //     
  },
  sendSMSCode: function () {
    this.setData({
      send_disabled: true
    })
    var that = this
    wx.request({
      url: 'this is your send sms sode url',
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, //       header
      success: function (res) {
        // success
        //      
        that.setData({
          send_time: Math.round(+new Date() / 1000)
        })
        that.sendCountDown()
      },
      fail: function () {
        // fail
        that.setData({
          send_disabled: false
        })
      },
      complete: function () {
        // complete
      }
    })
  },
  sendCountDown: function () {
    if (!this.data.send_time) {
      return
    }
    var seconds = this.data.send_time + 60 - Math.round(+new Date() / 1000)
    if (seconds > 0) {
      this.setData({
        send_text: `(${seconds}s)`
      })
      setTimeout(this.sendCountDown, 1000)
    } else {
      this.setData({
        send_text: '  ',
        send_disabled: false,
        send_time: null
      })
    }
  }
})

좋은 웹페이지 즐겨찾기