倒计时15分钟

微信小程序中,从一个页面点击跳到下一页,开始倒计时15分钟

//(1) wxml
<view class="box">
  <text class="time">{{time.M}}</text>
  <text class="time">{{time.m}}</text>
  <text></text>
  <text class="time">{{time.S}}</text>
  <text class="time">{{time.s}}</text>
</view>
//(2) js
// pages/my/index.js
//声明一个变量剩余时间
var remainingTime = 0;
var timer;
Page({
  data: {
      time: {
        M: '0',
        m: '0',
        S: '0',
        s: '0'
    },
  },

  
  onLoad: function () {
    remainingTime = Math.ceil( 900000  / 1000)
    this.timeCutDown()
  },
  timeCutDown() {
    timer = setTimeout(h => {
        if (remainingTime > 0) {
          remainingTime--
            this.timeCutDown()
        } else {
          remainingTime = 0;
            wx.showModal({
                title: '支付已超时,请重新下单',
                showCancel: false,
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                    if (result.confirm) {
                        wx.navigateBack({
                            delta: 1
                        });
                    }
                },
                fail: () => {},
                complete: () => {}
            });
        }
    }, 1000)
    this.timeFormat(remainingTime)
  },
  timeFormat(time) {
      if (time < 0) {
          time = 0
      }
      let min = Math.floor(time / 60);
      let second = time % 60;
      min = min < 10 ? "0" + min : "" + min;
      second = second < 10 ? "0" + second : "" + second;
      this.setData({
          'time.M': min.substring(0, 1),
          'time.m': min.substring(1, 2),
          'time.S': second.substring(0, 1),
          'time.s': second.substring(1, 2),
      })
  },
  onUnload: function () {
    clearTimeout(timer)
  },
  onHide: function () {
    clearTimeout(timer)
  },

  
})
// (3)css
/* pages/my/index.wxss */
.box{
  display: flex;
  align-items: center;
  width: 400rpx;
  margin: 0 auto;
}
.box .time{
    width: 48rpx;
    height: 48rpx;
    text-align: center;
    line-height: 48rpx;
    color: #fff;
    background: #656565;
    border-radius: 12rpx;
    margin: 0 10rpx;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值