网站底部加上网站运行时间的教程

首先,在Vue组件的标签中添加一个用于显示运行时间的元素,例如:

<div>网站运行了: {{ days }}天{{ hours }}小时{{ minutes }}分{{ seconds }}秒</div>

然后,在Vue组件的标签中定义计算属性和生命周期钩子函数,如下所示:

<script>
export default {
data() {
return {
runtime: null,
days: 0,
hours: 0,
minutes: 0,
seconds: 0
};
},
mounted() {
this.showRuntime();
},
methods: {
showRuntime() {
setInterval(() => {
const startTime = new Date("3/27/2024 7:00:00"); //改为创立站点时间
const currentTime = new Date();
const timeDifference = currentTime.getTime() - startTime.getTime();
const millisecondsPerDay = 24 * 60 * 60 * 1000;
this.days = Math.floor(timeDifference / millisecondsPerDay);
this.hours = Math.floor((timeDifference % millisecondsPerDay) / (60 * 60 * 1000));
this.minutes = Math.floor((timeDifference % (60 * 60 * 1000)) / (60 * 1000));
this.seconds = Math.floor((timeDifference % (60 * 1000)) / 1000);
}, 1000);
}
}
};
</script>

👋 感谢您的观看!

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享