Skip to content

Commit d9bfbd2

Browse files
committed
refactor: 增加 Day 逻辑
1 parent 7afab2d commit d9bfbd2

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/BootstrapBlazor/Components/FlipClock/FlipClock.razor.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function init(id, options) {
1414
return;
1515
}
1616

17+
const listDay = el.querySelector('.bb-flip-clock-list.day');
1718
const listHour = el.querySelector('.bb-flip-clock-list.hour');
1819
const listMinute = el.querySelector('.bb-flip-clock-list.minute');
1920
const listSecond = el.querySelector('.bb-flip-clock-list.second');
@@ -26,38 +27,36 @@ export function init(id, options) {
2627

2728
if (options.viewMode === "Count") {
2829
counter += 1000;
29-
// 计算累计时间(单位:毫秒)
3030
totalMilliseconds = counter - options.startValue;
3131
}
3232
else if (countDown) {
3333
counter += 1000;
34-
// 计算剩余时间(单位:毫秒)
3534
totalMilliseconds = options.startValue - counter;
36-
// 确保不会出现负数
3735
if (totalMilliseconds < 0) totalMilliseconds = 0;
3836
}
3937
else {
4038
now = new Date();
4139
return {
42-
days: now.getDate(), // 返回当前日期(月份中的第几天)
40+
days: now.getDate(),
4341
hours: now.getHours(),
4442
minutes: now.getMinutes(),
4543
seconds: now.getSeconds()
4644
};
4745
}
48-
// 将时间戳转换为天/时/分/秒
46+
4947
const seconds = Math.floor(totalMilliseconds / 1000) % 60;
5048
const minutes = Math.floor(totalMilliseconds / (1000 * 60)) % 60;
5149
const hours = Math.floor(totalMilliseconds / (1000 * 60 * 60)) % 24;
5250
const days = Math.floor(totalMilliseconds / (1000 * 60 * 60 * 24));
5351
return { days, hours, minutes, seconds };
5452
}
5553

54+
let lastDay;
5655
let lastHour;
5756
let lastMinute;
5857
let lastSecond;
5958
const go = () => {
60-
const { hours, minutes, seconds } = getDate();
59+
const { days, hours, minutes, seconds } = getDate();
6160

6261
if (lastSecond !== seconds) {
6362
lastSecond = seconds;
@@ -71,7 +70,11 @@ export function init(id, options) {
7170
lastHour = hours;
7271
setTime(listHour, hours, countDown);
7372
}
74-
return { hours, minutes, seconds }
73+
if (lastDay !== days) {
74+
lastDay = days;
75+
setTime(listDay, days, countDown);
76+
}
77+
return { days, hours, minutes, seconds }
7578
}
7679

7780
let start = void 0
@@ -95,7 +98,6 @@ export function init(id, options) {
9598
}
9699

97100
requestAnimationFrame(flip);
98-
99101
Data.set(id, { el, options });
100102
}
101103

0 commit comments

Comments
 (0)