Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/BootstrapBlazor/Components/FlipClock/FlipClock.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]

<div @attributes="@AdditionalAttributes" class="@ClassString" style="@StyleString" id="@Id">

@if (ShowDay)
{

Check warning on line 8 in src/BootstrapBlazor/Components/FlipClock/FlipClock.razor

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/FlipClock/FlipClock.razor#L8

Added line #L8 was not covered by tests
<div class="bb-flip-clock-list day">
@RenderItem(10)
@RenderItem(10)

Check warning on line 11 in src/BootstrapBlazor/Components/FlipClock/FlipClock.razor

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/FlipClock/FlipClock.razor#L10-L11

Added lines #L10 - L11 were not covered by tests
</div>
}

Check warning on line 13 in src/BootstrapBlazor/Components/FlipClock/FlipClock.razor

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/FlipClock/FlipClock.razor#L13

Added line #L13 was not covered by tests

@if (ShowHour)
{
<div class="bb-flip-clock-list hour">
Expand Down
5 changes: 5 additions & 0 deletions src/BootstrapBlazor/Components/FlipClock/FlipClock.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace BootstrapBlazor.Components;
/// </summary>
public partial class FlipClock
{
/// <summary>
/// 获得/设置 是否显示 Day 默认 false
/// </summary>
[Parameter]
public bool ShowDay { get; set; } = false;
/// <summary>
/// 获得/设置 是否显示 Hour 默认 true
/// </summary>
Expand Down
23 changes: 20 additions & 3 deletions src/BootstrapBlazor/Components/FlipClock/FlipClock.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,36 @@ export function init(id, options) {

let counter = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the time calculation logic to use a single helper and unified return shape for all branches.

You can collapse the three branches, pull your counter += 1000 out of each branch, and unify all of the “ms → days/hours/min/sec” logic into a single helper. This both removes duplication and guarantees you always return the same shape ({days,hours,minutes,seconds}).

// 1) single helper to break ms into parts
const splitMs = totalMs => {
  const days    = Math.floor(totalMs / 86_400_000);
  const hours   = Math.floor(totalMs / 3_600_000) % 24;
  const minutes = Math.floor(totalMs /   60_000) % 60;
  const seconds = Math.floor(totalMs /    1_000) % 60;
  return { days, hours, minutes, seconds };
};

let counter = 0;
const getDate = () => {
  // 2) increment once if we're in Count or CountDown
  if (options.viewMode === "Count" || options.viewMode === "CountDown") {
    counter += 1000;
  }

  let totalMs;

  switch (options.viewMode) {
    case "Count":
      // elapsed since start
      totalMs = counter - options.startValue;
      break;

    case "CountDown":
      // remaining until zero
      totalMs = Math.max(options.startValue - counter, 0);
      break;

    default:
      // real-time: ms since midnight, days will always be 0
      const now = new Date();
      const midnight = new Date(now).setHours(0, 0, 0, 0);
      totalMs = now - midnight;
  }

  return splitMs(totalMs);
};

With this:

  • counter += 1000 lives in one place.
  • You only do “ms → parts” once.
  • You always return {days,hours,minutes,seconds} so your rendering logic doesn’t need two shapes.

const getDate = () => {
let totalMilliseconds = 0;
let now;

if (options.viewMode === "Count") {
counter += 1000;
now = new Date(new Date().getTimezoneOffset() * 60 * 1000 - options.startValue + counter);
// 计算累计时间(单位:毫秒)
totalMilliseconds = counter - options.startValue;
}
else if (countDown) {
counter += 1000;
now = new Date(new Date().getTimezoneOffset() * 60 * 1000 + options.startValue - counter);
// 计算剩余时间(单位:毫秒)
totalMilliseconds = options.startValue - counter;
// 确保不会出现负数
if (totalMilliseconds < 0) totalMilliseconds = 0;
}
else {
now = new Date();
return {
days: now.getDate(), // 返回当前日期(月份中的第几天)
hours: now.getHours(),
minutes: now.getMinutes(),
seconds: now.getSeconds()
};
}
return { hours: now.getHours(), minutes: now.getMinutes(), seconds: now.getSeconds() };
// 将时间戳转换为天/时/分/秒
const seconds = Math.floor(totalMilliseconds / 1000) % 60;
const minutes = Math.floor(totalMilliseconds / (1000 * 60)) % 60;
const hours = Math.floor(totalMilliseconds / (1000 * 60 * 60)) % 24;
const days = Math.floor(totalMilliseconds / (1000 * 60 * 60 * 24));
return { days, hours, minutes, seconds };
}

let lastHour;
Expand Down