多个活动倒计时(小程序版)

大佬请绕路

calculateTime(that, time, fn = () => { }, fn1 = () => { },t = false) {
        //that  页面this
        //time  当前时间
        //fn与fn1 可以不写
        //t 当没有时间传入时获取时间↓ ↓ ↓ ↓ ↓ 
        if(t === false){
            let nowTime = that.nowtime = that.nowtime + 1000;
            t = Math.abs(nowTime - time);
        }
        //获取天  时 分 秒
        let t1 = Number.parseInt(t / (1000 * 60 * 60 * 24));
        let t2 = Number.parseInt((t % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        let t3 = Number.parseInt(((t % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60));
        let t4 = Number.parseInt((((t % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) % (1000 * 60)) / (1000));
        t1 = t1 <= 9 == 1 ? `0${t1}` : t1 == 0 ? "00" : t1;
        t2 = t2 <= 9 == 1 ? `0${t2}` : t2 == 0 ? "00" : t2;
        t3 = t3 <= 9 == 1 ? `0${t3}` : t3 == 0 ? "00" : t3;
        t4 = t4 <= 9 ? `0${t4}` : t4 == 0 ? "00" : t4;
        fn1([t1, t2, t3, t4]);
        fn();
    },
    //好货仓  倒计时
    getTimeList(that) {
        // let supplySquareList = that.data.supplySquareList;
        //这里数据写死 到时候要从后端获取  具体数据结构可根据自己的来
        let supplySquareList = [
            {
                time_type:{
                    start_:"2019-12-03 17:21",
                    end_:"2019-12-04 14:21"
                }
            },
            {    time_type:{
                    start_:"2019-11-04 11:42",
                    end_:"2019-11-04 17:25"
                }
            }
        ];
        let timeList = [];
        //从活动列表中  生成一个时间列表 (活动列表数据太多)
        supplySquareList.map(v => {
            let data = {
                time_type: [],
                isLongTime: true,
                title: "距开始",
                countDownValue: [0, 0, 0, 0]
                              //[天,时,分,秒]
                              //结束时['00','00','00','00']
            };
            let time_type = v.time_type;
            if (time_type && time_type.start_) {
            //ios 时间与安卓不同 要处理一下
                time_type.start = new Date(time_type.start_.replace(/-/g, "/")).getTime();
                time_type.end = new Date(time_type.end_.replace(/-/g, "/")).getTime();
                data.time_type = time_type;
                data.isLongTime = false;

                timeList.push(data);
            }
        })
        return timeList;
    },
    initTimeList(that) {
        //入口函数
        let timeList = this.getTimeList(that);
        let Interval = setInterval(()=>{
            that.nowTime = that.nowTime +  1000;
            // console.log(that.nowTime);
            this.setTimeListStatus(that,timeList)
        },1000);
        
        that.Interval = Interval;
    },
    setTimeListStatus(that, timeList) {
        let nowTime = that.nowTime;// 当前时间从服务器获取
        
        let time = 0;
        timeList.map(v => {
            //活动已开始
            if (nowTime > v.time_type.start) {
                //活动已经结束
                if (nowTime > v.time_type.end) {
                    v.title = "活动已结束";
                    v.isLongTime = true;
                } else {
                    //活动进行中
                    time = Math.abs(v.time_type.end - nowTime);
                    v.title = "距结束";
                    v.isLongTime = false;
                }
            }else{
                //活动未开始
                time = Math.abs(v.time_type.start - nowTime);
                v.title = "距开始",
                v.isLongTime = false;
            }
            if(!v.isLongTime){
                this.calculateTime(that,time,()=>{},(data)=>{
                    v.countDownValue = data;
                },time);
            }
        })
        this.clearInterval(that,timeList);
    },
    clearInterval(that,timeList){
        let isClear = true;
        //检查所有活动  是否都结束
        timeList.map(v=>{
            if(!v.isLongTime){
                isClear = false;
            }
        })
        //更新数据
        that.setData({
            timeList
        })
        if(isClear){
        //所有活动停止时关闭定时器
            clearInterval(that.Interval);
        }
        
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值