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>