效果
![image.png](https://ucc.alicdn.com/pic/developer-ecology/5xav467jynvqu_534d5a69b3704140bc9fbf0666f92dd8.png?x-oss-process=image/resize,w_1400/format,webp)
代码实现
<template>
<div>倒计时: {
{
keepTime }}</div>
</template>
<script>
export default {
data() {
return {
keepTime: "",
};
},
created() {
this.StartCountDown();
},
methods: {
StartCountDown() {
var mydate = new Date();
mydate.setMinutes(mydate.getMinutes() + 90);
this.settime = mydate;
let time = setInterval(() => {
if (this.flag == true) {
clearInterval(time);
}
this.timeDown();
}, 100);
},
timeDown() {
const endTime = new Date(this.settime);
const nowTime = new Date();
let leftTime = parseInt((endTime.getTime() - nowTime.getTime()) / 1000);
let d = parseInt(leftTime / (24 * 60 * 60));
let h = this.formate(parseInt((leftTime / (60 * 60)) % 24));
let m = this.formate(parseInt((leftTime / 60) % 60));
let s = this.formate(parseInt(leftTime % 60));
if (leftTime <= 0) {
}
this.keepTime = `${h}:${m}:${
s}`;
},
formate(time) {
if (time >= 10) {
return time;
} else {
return `0${
time}`;
}
},
},
};
</script>
<style>
</style>