vue 定时刷新 当前时间
<div>{{ currDate }}</div>
data() {
return {
timer: '',
currDate: new Date()
}
},
created() {
const appendZero = (obj) => {
if (obj < 10) {
return '0' + obj
} else {
return obj
}
}
const getCurrTime = () => {
this.currDate =
new Date().getFullYear() + '年' +
appendZero(new Date().getMonth() + 1) + '月' +
appendZero(new Date().getDate()) + '日' +
appendZero(new Date().getHours()) + ' : ' +
appendZero(new Date().getMinutes()) + ': ' +
appendZero(new Date().getSeconds())
}
getCurrTime();
this.timer = setInterval(() => {
getCurrTime();
}, 1000);
},
beforeDestroy() {
if(this.timer) {
clearInterval(this.timer)
}
}
最终效果:
网友评论