手写一个分:秒:毫秒格式的倒计时
直接上源码:
<template>
<div>
<div>
{{ t_Minutes }} : {{ t_Seconds }} : {{ t_Milliseconds }}
</div>
<div v-if="time <= 0">时间到了!</div>
</div>
</template>
<script>
export default {
data() {
return {
time: 180,
};
},
computed: {
t_Minutes() {
const minutes = Math.floor(this.time / 60);
return minutes.toString().padStart(2, '0');
},
t_Seconds() {
const seconds = Math.floor(this.time % 60);
return seconds.toString().padStart(2, '0');
},
t_Milliseconds() {
const milliseconds = Math.floor((this.time % 1) * 100);
return milliseconds.toString().padStart(2, '0');
},
},
mounted() {
setInterval(this.updateTime, 1);
},
methods: {
updateTime() {
if (this.time > 0) {
this.time -= 0.004;
}
},
},
};
</script>