一、代码如下
<templete>
<div class="auctionnowlist" v-for="(item) in nowspec" :key="item.specId">
<div class="auctionnowlistleft">
<img :src="item.hfileIds" class="auctionnowlistimg" />
</div>
<div class="auctionnowlistcen">
<p class="cennamezc">{{item.specName}}</p>
<p class="centime">
<span>距开始结拍</span>
<span style="margin-left:5px;color:#A43534;font-weight:450">{{timeChange(item.countDown)}}</span>
</p>
<p class="cenjp" :class="[item.status=='拍卖中'?'red':'']">{{item.status}}</p>
</div>
<div class="auctionnowlistright">
<button class="gonumzc" @click="gotospec(item)">参与竞拍</button>
</div>
</div>
</templete>
<script>
export default({
data(){
return{
nowspec:[],
timer:null,
}
},
created(){
if (this.timer) { //这一段是防止进入页面出去后再进来计时器重复启动
clearInterval(this.timer);
}
},
mounted(){
this.getlist();
this.beginTimer();
},
methods:{
getlist(){
//获取返回的列表数据
this.$axios({
method: "get",
url:baseUrl+"/test",//这里是请求接口
}).then(res => {
this.nowspec = res.data.data;
this.nowspec===null?this.nowspec = []:this.nowspec = this.nowspec;
})
},
beginTimer() { //这个计时器是每秒减去数组中指定字段的时间
this.timer = setInterval(() => {
for (let i = 0, len = this.nowspec.length; i < len; i++) {
const item = this.nowspec[i];
if (item.countDown > 0) {
item.countDown = item.countDown - 1;//我这里后台返回的是秒数,如果是日期需要自己转换一下
}
}
}, 1000);
},
timeChange(countDown) {
var $that = this;
if (countDown < 0) {
clearInterval(setInterval(this.timeChange(countDown), 1000)); //当无剩余时间了,就停止执行timeChange函数
}
//求天时分秒 leftDate是毫秒
let leftSecond = $that.fillNumber(countDown % 60);
let leftMinute = $that.fillNumber((countDown / 60) % 60);
let leftHours = $that.fillNumber((countDown / (60 * 60)) % 24);
let leftDay = $that.fillNumber(countDown / (60 * 60) / 24);
return leftDay+" 天 "+leftHours+" 小时 "+leftMinute+" 分 "+leftSecond+" 秒 ";
},
fillNumber(num) {
let number = Math.floor(num); //向下取整
return number > 9 ? number : "0" + number;
},
}
})
</script>
二、效果如下