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):
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).
HTML simulator preview (HarmonyOS / ArkUI look), not a device render
// 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).
Top: `DD天HH:mm:ss.S` (86401500ms); bottom: `HH:mm:ss` without DD carries total hours (181810000ms)
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.
Top: controlled display of value=3665000; buttons drive an uncontrolled countdown via LhCountdownController
// 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.
Disabled state greys the text and pauses the countdown (golden case disabled-pauses-and-greys)
LhCountdown({ time: 600000, disabled: true })API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
value | Remaining milliseconds (controlled display: with a value the component does not drive itself; the host advances and finishes) | number | — |
time | Uncontrolled total duration ms (runs on mount when autoStart) | number | 0 |
format | Format 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' |
autoStart | Whether the uncontrolled mode starts on mount | boolean | true |
millisecond | Frame-rate refresh (recommended when rendering milliseconds; 16ms steps, otherwise 250ms) | boolean | false |
disabled | Disabled: greys and pauses the countdown | boolean | false |
ariaLabel | Accessibility name (falls back to the formatted text) | string | — |
onChange | Remaining milliseconds change (uncontrolled every step; controlled re-writes go through this event too) | (value: number) => void | — |
onFinish | Fires once when the countdown reaches zero | () => void | — |
controller | Host 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:
| Method | Description |
|---|---|
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
| Token | Usage |
|---|---|
countdown.digitMinWidth | Minimum digit width (18vp, equal-width anti-jitter) |
color.text.primary | Digit text color |
color.text.secondary | De-emphasized separator color |
color.text.disabled | Disabled text color |
typography.fontSize.base | Digit 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 id | Assertion | Local test |
|---|---|---|
renders-formatted-remaining | Controlled value renders in the default HH:mm:ss format | renders_formatted_remaining |
negative-clamps-to-zero | Negative values clamp to all zeros | negative_clamps_to_zero |
hours-over-24-without-dd | Without DD, HH carries the total hours | hours_over_24_without_dd |
dd-format-splits-days | DD splits days; milliseconds zero-padded to three digits | dd_format_splits_days |
uncontrolled-autostart-ticks | Uncontrolled autoStart runs on mount and emits onChange | uncontrolled_autostart_ticks |
finish-emits-once-at-zero | Reaching zero emits onFinish once | finish_emits_once_at_zero |
controlled-does-not-self-drive | Controlled mode does not drive itself | controlled_does_not_self_drive |
autostart-false-waits-for-start | autoStart=false waits for start() | autostart_false_waits_for_start |
disabled-pauses-and-greys | Disabled greys and does not drive | disabled_pauses_and_greys |
aria-role-timer | role=timer with the accessible name falling back to the formatted text | aria_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.

