<script>
function countDownFun(time) {
time--; //時間一秒秒的減
let nowTime = new Date().getTime(); //現在時間
if (nowTime <= time) {
//獲取時間差
let timediff = Math.round((time - nowTime) / 1000);
//獲取還剩多少天
let day = parseInt(timediff / 3600 / 24);
//獲取還剩多少小時
let hour = parseInt((timediff / 3600) % 24);
//獲取還剩多少分鍾
let minute = parseInt((timediff / 60) % 60);
//獲取還剩多少秒
let second = timediff % 60;
return day + "天" + hour + "小時" + minute + "分" + second + "秒";
} else {
return "00天00小時00分00秒";
}
}
export default {
name: "meizhoupintuan",
async created() {
let data = await home_meizhou_api();
this.list = data.data.list;
this.timer();
},
data() {
return {
list: [],
temp: null //倒計時初始
};
},
methods: {
timer() {
//頁面多個定時器 //主要邏輯都在這頁面更新
let _that = this;
this.temp = setInterval(() => {
this.list.forEach((item, index) => {
item.dayTime = countDownFun(item.endAt);
this.$set(this.list, item.dayTime, countDownFun(item.endAt));
console.log(this.temp, "6");
});
}, 1000);
},
},
destroyed() {
//切記頁面銷毀需要銷毀
clearInterval(this.temp);
console.log(this.temp, "銷毀");
}
};
</script>