vue定时器问题(单个定时器和循环定时器)

有两种情况:
一、单个定时器,比如发送验证码后,显示倒计时60秒
二、在循环中创建多个定时器,比如美团未支付页面有多个订单,都是从下单时间开始倒计时15分钟
第二个情况有点复杂,弄了很久,算是踩坑了。

一、单个定时器,比如发送验证码后,显示倒计时60秒
html

        <div class="input-item">
            <span class="des">验证码</span>
            <input class="input" placeholder="填写验证码" id="validateCode" />
            <a href="javascript:void(0)" v-on:click="GetverificationCode">{{count}}</a>
        </div>

js

        data: {
            count: "获取验证码",
            timer: null,//关键
        },
        methods:{
	 //获取验证码
            GetverificationCode: function (e) {
                let that = this;
                $.ajax({
                    url: "/OrderMeal/SendVerificateCode",
                    data: {
                        phone: $("#phone").val()
                    },
                    success: function (data) {
                        if (data.isSuccess == false) {
                            $.toptip('系统繁忙,请稍后再试', 'warning');
                            return;
                        }
                        const TIME_COUNT = 60;//设置60秒
                        if (!that.timer) {
                            that.count = TIME_COUNT;
                            that.show = false;
                            that.timer = setInterval(() => {
                                if (that.count > 0 && that.count <= TIME_COUNT) {
                                    that.count--;
                                } else {
                                    clearInterval(that.timer);
                                    that.timer = null;
                                    that.count = "重新获取";
                                }
                            }, 1000)
                        }
                    }
                })
            },
},

二、在循环中创建多个定时器,比如美团未支付页面有多个订单,都是从下单时间开始倒计时15分钟

for (var i = 0; i < that.orderList.length; i++) {
       let j = i;//重点
       (function (j) {//重点,跟循环打印一个道理,否则得到的的是最后一个,而不是每一个值都有
       that.orderList[j].timer = null;//重点
       var startTime = new Date().getTime();
       var endTime = Date.parse(that.orderList[j].orderTime);//下单时间换算成毫秒
       endTime += 1800000//加30分钟(倒计时三十分钟)
       var leftTime = endTime - startTime;//下单后的30分钟-现在的时间
       if (!that.orderList[j].timer) {
       that.orderList[j].timer = setInterval(() => {
            if (leftTime > 0) {
            leftTime = leftTime - 1000
            var m = Math.floor(leftTime / 1000 / 60 % 60);
            var s = Math.floor(leftTime / 1000 % 60);
            that.$set(that.orderList[j], "Min", m)//上一篇有讲,给数组对象加新属性
            that.$set(that.orderList[j], "Sec", s)
            //console.log(that.orderList[j].Min, s)
             } else {
            clearInterval(that.orderList[j].timer);
          	that.orderList[j].timer = null;
           }
        }, 1000)
    }
 })(j)
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值