data() {
toLiveBtn: "已到期",
_interval: ""
};
},
//把时间日期转成时间戳 ios不支持2019-1-29 17:13:04 这种"-"的格式 所以就提前转换格式然后再进行秒数转换
let nowtime = res.time.replace(/-/g, "/"); //转换格式
let oldtime = res.expire_time.replace(/-/g, "/"); //转换格式
let time = new Date(nowtime).getTime(); //当前时间
let endtime = new Date(oldtime).getTime(); //到期时间
let surplus = (endtime - time) / 1000; //剩余时间
const that = this;
that._interval = setInterval(() => {
if (surplus == 0) {
// 计时结束,清除缓存
clearInterval(that._interval);
} else {
surplus--;
let day = parseInt(surplus / 60 / 60 / 24);
let hr = parseInt((surplus / 60 / 60) % 24);
let min = parseInt((surplus / 60) % 60);
let sec = parseInt(surplus % 60);
day = day > 9 ? day : "0" + day;
hr = hr > 9 ? hr : "0" + hr;
min = min > 9 ? min : "0" + min;
sec = sec > 9 ? sec : "0" + sec;
that.toLiveBtn = `${day}天${hr}时${min}分${sec}秒`;
}
}, 1000);
//当离开页面时,清除倒计时
beforeDestroy() {
clearInterval(this._interval);
}