vue + axios 中倒计时处理;

vue 外部函数处理:
function InitTime(endtime){
    var dd,hh,mm,ss = null;
    var time = parseInt(endtime) - parseInt(new Date().getTime());
    /*console.log(endtime);
    console.log(new Date().getTime())*/
    if(time<=0){
        return '结束'
    }else{
        dd = Math.floor(time / 60 / 60 / 24);
        hh = Math.floor((time / 60 / 60) % 24);
        mm = Math.floor((time / 60) % 60);
        ss = Math.floor(time  % 60);
        var str = dd+"天"+hh+"小时"+mm+"分"+ss+"秒";
        return str;
    }
}
mounted() {
    setInterval( ()=> {
        for (var key in this.activeItems) {
            if(this.activeItems[key].in_rush){
                var aaa = parseInt( this.activeItems[key]["rush_expired_time"] );
                var bbb = new Date().getTime();
                var rightTime = aaa - bbb;
                if (rightTime > 0) {
                    var dd = Math.floor(rightTime / 1000 / 60 / 60 / 24);
                    var hh = Math.floor((rightTime / 1000 / 60 / 60) % 24);
                    var mm = Math.floor((rightTime / 1000 / 60) % 60);
                    var ss = Math.floor((rightTime / 1000) % 60);
                    if(dd<10){ dd="0"+dd;}
                    if(hh<10){ hh="0"+hh;}
                    if(mm<10){ mm="0"+mm;}
                    if(ss<10){ ss="0"+ss;}
                    this.activeItems[key]["djs"] = "<span>"+dd + "</span>天<span>" + hh + "</span>:<span>" + mm + "</span>:<span>" + ss + "</span>";
                }
            }else{
                this.activeItems[key]["djs"] = '';
            }
        }
    }, 1000);
},

在 methods:使用

data 参数数据:

loading:false,
finished:false,
activeItems:[],//活动专区列表
firstactive:false,
//取活动专题栏:
getActives:function(){
    var that = this;
    that.loading = true;
    that.finished = false;
    axios.get('/api/items/activity').then(function(res){
        that.loading = false;
        that.finished = true;
        if(res.data.code == 0){
          //  console.log(res.data.data);
            res.data.data.map( (obj,index)=>{
                that.$set(
                    obj,"djs",InitTime(obj.rush_expired_time)
                );
            });
            that.activeItems=res.data.data;
            if(that.activeItems.length==0){
                that.firstactive=true;
            }else{
                that.firstactive=false;
            }
        }
    })
},

html:

<div class="item-djs" v-if="item.in_rush" v-html="'距结束仅剩'+item.djs"></div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值