countdown.js
3.4 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// components/countdown/countdown.js
Component({
/**
* 页面的初始数据
*/
data: {
day: "00",
day_one: "0",
day_two: "0",
hour: "00",
hour_one: "0",
hour_two: "0",
minute: "00",
minute_one: "0",
minute_two: "0",
second: "00",
second_one: "0",
second_two: "0",
},
properties: {
justifyLeft: {
type: String,
value: ""
},
//距离开始提示文字
tipText: {
type: String,
value: "倒计时"
},
dayText: {
type: String,
value: "天"
},
hourText: {
type: String,
value: "时"
},
minuteText: {
type: String,
value: "分"
},
secondText: {
type: String,
value: "秒"
},
datatime: {
type: Number,
value: 0
},
isDay: {
type: Boolean,
value: true
}
},
created: function () {
this.show_time();
},
methods: {
show_time: function () {
let that = this;
function runTime() {
//时间函数
let intDiff = that.data.datatime - Date.parse(new Date()) / 1000; //获取数据中的时间戳的时间差;
// console.log(that.data.datatime + '结束时间', intDiff + '时间差', Date.parse(new Date()) + '当前时间')
let day = 0,
hour = 0,
minute = 0,
second = 0;
if (intDiff > 0) {
// console.log('时间差大于0')
//转换时间
if (that.data.isDay === true) {
// console.log('时间转换开始')
day = Math.floor(intDiff / (60 * 60 * 24));
} else {
day = 0;
}
// console.log(day + '格式化后的天数')
hour = Math.floor(intDiff / (60 * 60)) - day * 24;
// console.log(hour + '格式化后的小时')
minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60;
// console.log(minute + '格式化后的分钟')
second =
Math.floor(intDiff) -
day * 24 * 60 * 60 -
hour * 60 * 60 -
minute * 60;
// console.log(second + '格式化后的秒')
if (day <= 9) day = "0" + day;
if (hour <= 9) hour = "0" + hour;
if (minute <= 9) minute = "0" + minute;
if (second <= 9) second = "0" + second;
// console.log('格式化完成' + day + '天' + hour + '小时' + minute + '分' + second + '秒')
that.setData({
day: day,
day_one: day.toString().substr(0, 1),
day_two: day.toString().substr(1, 2),
hour: hour,
hour_one: hour.toString().substr(0, 1),
hour_two: hour.toString().substr(1, 2),
minute: minute,
minute_one: minute.toString().substr(0, 1),
minute_two: minute.toString().substr(1, 2),
second: second,
second_one: second.toString().substr(0, 1),
second_two: second.toString().substr(1, 2)
})
} else {
that.setData({
day: '00',
day_one: '0',
day_two: '0',
hour: '00',
hour_one: '0',
hour_two: '0',
minute: '00',
minute_one: '0',
minute_two: '0',
second: '00',
second_one: '0',
second_two: '0'
})
}
}
runTime();
setInterval(runTime, 1000);
}
}
})