Skip to content

Countdown ​

A wall-clock-difference-driven countdown: no drift when the app is backgrounded or the timer is throttled. Controlled display and uncontrolled self-running modes, format tokens DD / HH / mm / ss / S; instance methods start / pause / reset are exposed as a handle.

Spec source: lh-ui-core/specs/countdown.json · Golden cases: lh-ui-core/cases/countdown.json (10 cases, translated into hypium tests)

Device-side implementation note

The remaining time is derived from a wall-clock difference (target wall clock − now, clamped ≥ 0) instead of a decrementing accumulator: the deadline is recorded once at start / reset and every step only compares against it, so backgrounding, timer throttling or page switches never introduce accumulated drift. The uncontrolled mode is driven by setTimeout steps (16ms when millisecond is true, otherwise 250ms).

The spec's instance methods start / pause / reset land on the device side as an LhCountdownController handle (ArkUI does not provide references to child component instances, same approach as LhToastController):

typescript
private countdownCtl: LhCountdownController = new LhCountdownController()

build() {
  Column() {
    LhCountdown({ time: 60000, controller: this.countdownCtl })
    LhButton({ text: 'Pause', lhOnClick: () => { this.countdownCtl.pause() } })
  }
}

A provided value means controlled: the component does not drive itself and displays the external value; a provided time means uncontrolled: with autoStart it starts running on mount. The spec's default slot (trailing content) has no slot counterpart in the device component; the host can compose it in a Row (same approach as the rest of this library's components).

Basic usage ​

Uncontrolled mode: time sets the total duration and autoStart runs it on mount. Digits are equal-width (countdown.digitMinWidth token); separators are de-emphasized (color.text.secondary).

9:41Countdown basic usage
01:00:00
00.500

HTML simulator preview (HarmonyOS / ArkUI look), not a device render

typescript
// Uncontrolled 60 seconds (default HH:mm:ss format)
LhCountdown({ time: 60000 })

// Enable millisecond when rendering the S token (16ms steps)
LhCountdown({ time: 10000, format: 'mm:ss.S', millisecond: true })

Format tokens ​

The format string is case-sensitive: only DD / HH / mm / ss / S are tokens, everything else renders literally. When DD is present, HH carries only 0-23; without DD, HH carries the total hours (may exceed 24). S is zero-padded to three digits (0-999). Negative / invalid values always display as 0 (golden case negative-clamps-to-zero).

9:41Countdown format tokens
01天00:00:01.500
50:30:10

Top: `DD天HH:mm:ss.S` (86401500ms); bottom: `HH:mm:ss` without DD carries total hours (181810000ms)

typescript
LhCountdown({ value: 86401500, format: 'DD天HH:mm:ss.S' })  // 01天00:00:01.500
LhCountdown({ value: 181810000, format: 'HH:mm:ss' })        // 50:30:10 (total hours)

Controlled and uncontrolled ​

A provided value means controlled: the component does not drive itself and displays the external value; the host is responsible for advancing and finishing (golden case controlled-does-not-self-drive). A provided time means uncontrolled: onChange reports the remaining milliseconds on every step, and onFinish fires once when it reaches zero, then the ticker stops.

9:41Countdown controlled and uncontrolled
01:01:05

Top: controlled display of value=3665000; buttons drive an uncontrolled countdown via LhCountdownController

typescript
// Controlled display (the host advances the value via onChange)
LhCountdown({ value: this.remaining, onChange: (v: number) => { this.remaining = v } })

// Uncontrolled + autoStart=false: waits for start()
LhCountdown({ time: 60000, autoStart: false, controller: this.countdownCtl })

Disabled ​

disabled = true greys the text (color.text.disabled) and pauses the countdown; the handle's start() has no effect while disabled. When re-enabled, a countdown that was running resumes automatically.

9:41Countdown disabled
00:10:00

Disabled state greys the text and pauses the countdown (golden case disabled-pauses-and-greys)

typescript
LhCountdown({ time: 600000, disabled: true })

API ​

Props ​

PropDescriptionTypeDefault
valueRemaining milliseconds (controlled display: with a value the component does not drive itself; the host advances and finishes)number—
timeUncontrolled total duration ms (runs on mount when autoStart)number0
formatFormat string; tokens DD (days) / HH (hours) / mm (minutes) / ss (seconds) / S (milliseconds, 3 digits); other characters render literally; without DD, HH carries the total hours (may exceed 24)string'HH:mm:ss'
autoStartWhether the uncontrolled mode starts on mountbooleantrue
millisecondFrame-rate refresh (recommended when rendering milliseconds; 16ms steps, otherwise 250ms)booleanfalse
disabledDisabled: greys and pauses the countdownbooleanfalse
ariaLabelAccessibility name (falls back to the formatted text)string—
onChangeRemaining milliseconds change (uncontrolled every step; controlled re-writes go through this event too)(value: number) => void—
onFinishFires once when the countdown reaches zero() => void—
controllerHost handle (plain property, not @Prop)LhCountdownController—

LhCountdownController ​

Handle methods (no effect in controlled mode). The component registers its internal methods on the handle in aboutToAppear:

MethodDescription
start()Resumes from the pause point (no effect while disabled)
pause()Pauses and remembers the remaining time
reset(timeMs?)Returns to the time total duration (optionally a new one) and may restart per autoStart

Tokens ​

TokenUsage
countdown.digitMinWidthMinimum digit width (18vp, equal-width anti-jitter)
color.text.primaryDigit text color
color.text.secondaryDe-emphasized separator color
color.text.disabledDisabled text color
typography.fontSize.baseDigit font size (countdown has no component-level font-size token)

Golden cases ​

The 10 assertions of cases/countdown.json mapped to hypium tests (entry/src/test/Countdown.test.ets):

case idAssertionLocal test
renders-formatted-remainingControlled value renders in the default HH:mm:ss formatrenders_formatted_remaining
negative-clamps-to-zeroNegative values clamp to all zerosnegative_clamps_to_zero
hours-over-24-without-ddWithout DD, HH carries the total hourshours_over_24_without_dd
dd-format-splits-daysDD splits days; milliseconds zero-padded to three digitsdd_format_splits_days
uncontrolled-autostart-ticksUncontrolled autoStart runs on mount and emits onChangeuncontrolled_autostart_ticks
finish-emits-once-at-zeroReaching zero emits onFinish oncefinish_emits_once_at_zero
controlled-does-not-self-driveControlled mode does not drive itselfcontrolled_does_not_self_drive
autostart-false-waits-for-startautoStart=false waits for start()autostart_false_waits_for_start
disabled-pauses-and-greysDisabled greys and does not drivedisabled_pauses_and_greys
aria-role-timerrole=timer with the accessible name falling back to the formatted textaria_label_falls_back_to_formatted (accessibility-tree assertion verified on device)

Additional dark-mode cases: dark_disabled_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode, plus pause_and_reset_keep_remaining / reset_accepts_new_time (the start/pause/reset handle core). Run: DevEco Studio, right-click entry/src/test → Run Tests.

lh-ui native bloodline · HarmonyOS (ArkTS + ArkUI)