计时器 分秒倒计时

html里面:    

<text class="time">支付剩余时间 {
   
   {minutes<10 ? ('0' + minutes) : minutes }}:{
   
   {seconds<10 ? ('0' + seconds) : seconds }}</text>

data里面定义变量:

minutes: 10, //分钟
seconds: 0, //秒数

在加载函数里面调用:

onLoad() {
    this.countDownTimer()
    },

在方法methods里面封装方法:

   // 分秒倒计时
                countDownTimer() {
                    let _this=this
                    let time = setInterval(function() {
                        if (_this.seconds === 0 && _this.minutes !== 0) {
                            _this.seconds = 59
                            _this.minutes --
                        } else if (_this.minutes === 0 && _this.seconds === 0) {
                            _this.seconds = 0
                             _this.minutes=0
                            clearInterval(time)
                        }else {
                            _this.seconds --
                        }
                    }, 1000)
                },


 
 

猜你喜欢

转载自blog.csdn.net/m0_56276571/article/details/125913865