Vue倒计时代码–适用于未支付订单倒计时1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38data() {
return {
timeLeft: ""
};
},
//在mounted中调用
mounted() {
setInterval(this.countTime, 1000);
},
//方法如下
methods: {
countTime() {
//获取当前时间
let date = new Date();
let now = date.getTime();
//设置截止时间
//let end = new Date(this.detail.CreatedDate).getTime() + 15 * 60 * 1000;//this.detail.CreatedDate可以是后端返回的创建时间
let end = new Date("2018-05-11 15:02").getTime() + 15 * 60 * 1000;
//console.log(end, "结束时间");
//时间差
let leftT = end - now;
//console.log(leftT, "时间差");
//定义变量 d,h,m,s保存倒计时的时间
let d, h, m, s;
if (leftT >= 0) {
//d = Math.floor(leftT / 1000 / 60 / 60 / 24);
//h = Math.floor((leftT / 1000 / 60 / 60) % 24);
m = Math.floor((leftT / 1000 / 60) % 60);
s = Math.floor((leftT / 1000) % 60);
} else {
m = 0;
s = 0;
}
clearInterval(this.countTime);
//将倒计时赋值到data中
this.timeLeft = `${m}分${s<10 ? `0${s}` : `${s}` }秒`;
}
}
1 | var timerHourDown; |