
倒计时(Vue)
本文最后更新于 2025-07-15,文章内容可能已经过时。
startCountdown(customEndTime) {
const endTime = customEndTime || this.endTime;
if (!endTime) {
console.warn('未设置结束时间');
return;
}
const updateFn = () => this.updateCountdown(endTime);
this.updateCountdown(endTime);
this.timer = setInterval(updateFn, 1000);
},
updateCountdown(endTime) {
const now = moment();
const end = moment(endTime);
const diff = end.diff(now);
if (diff <= 0) {
// 清除定时器
clearInterval(this.timer);
this.leaveTimeShow = false;
this.countdown = { days: 0, hours: 0, minutes: 0, seconds: 0 };
return;
}
this.leaveTimeShow = true;
const duration = moment.duration(diff);
this.countdown = {
days: Math.floor(duration.asDays()),
hours: duration.hours(),
minutes: duration.minutes(),
seconds: duration.seconds()
};
},
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果