CountDown

introduce

Used to display countdown values in real time, supporting millisecond precision.

Code Demo

Basic usage

The time attribute represents the total countdown time, in milliseconds.

<z-count-down :time="time" />
import { ref } from 'vue';
const time = ref(30 * 60 * 60 * 1000)

Custom format

Set the content of the countdown text through the format attribute.

<z-count-down :time="time" format="DD days HH hours mm minutes ss seconds" />

Millisecond rendering

The countdown is rendered once per second by default. Setting the millisecond property can enable millisecond rendering.

<z-count-down millisecond :time="time" format="HH:mm:ss:SS" />

Custom style

Customize the countdown style through slots. See the table below for the timeData object format.

<z-count-down :time="time">
   <template #default="{ current }">
     <text class="block">{{ current.hours }}</text>
     <text class="colon">:</text>
     <text class="block">{{ current.minutes }}</text>
     <text class="colon">:</text>
     <text class="block">{{ current.seconds }}</text>
   </template>
</z-count-down>

<style>
   .colon {
     display: inline-block;
     margin: 0 8rpx;
     color: var(--z-primary-color);
   }

   .block {
     display: inline-block;
     width: 44rpx;
     font-size: 24rpx;
     color: #fff;
     text-align: center;
     background-color: var(--z-primary-color);
     border-radius: 8rpx;
   }
</style>

Manual control

After obtaining the component instance through ref, you can call the start, pause, and reset methods.

<view class="count-content">
   <z-count-down
     ref="countDown"
     millisecond
     :time="3000"
     :auto-start="false"
     format="ss:SSS"
     @finish="onFinish"
   />
</view>
<z-grid clickable :column-num="3">
   <z-grid-item icon="play-circle" text="Start" @click="start" />
   <z-grid-item icon="timeout" text="Pause" @click="pause" />
   <z-grid-item icon="reload" text="Reset" @click="reset" />
</z-grid>
import { ref } from 'vue'
import { useToast } from '../../uni_modules/zebra-ui'
const toast = useToast()
const countDown = ref()
const start = () => {
   countDown.value?.start()
}
const pause = () => {
   countDown.value?.pause()
}
const reset = () => {
   countDown.value?.reset()
}
const onFinish = () => toast.showToast('Countdown is over')

API

Props

ParametersDescriptionTypeDefault value
timeCountdown duration in millisecondsnumber | string0
formattime formatstringHH:mm:ss
auto-startWhether to automatically start the countdownbooleantrue
millisecondWhether to enable millisecond renderingbooleanfalse

format format

FormatDescription
DDNumber of days
HHhours
mmminutes
ssseconds
SMilliseconds (1 bit)
SSmilliseconds (2 digits)
SSSmilliseconds (3 digits)

Events

Event nameDescriptionCallback parameters
finishTriggered when countdown ends-
changeTriggered when the countdown changescurrentTime: CurrentTime

Slots

NameDescriptionParameters
defaultCustom contentcurrentTime: CurrentTime

CurrentTime format

NameDescriptionType
totaltotal remaining time (unit: milliseconds)number
daysremaining daysnumber
hoursremaining hoursnumber
minutesminutes remainingnumber
secondsNumber of seconds remainingnumber
millisecondsRemaining millisecondsnumber

method

Through ref, you can get the CountDown instance and call the instance method.

Method nameDescriptionParametersReturn value
startStart countdown--
pausePause countdown--
resetReset the countdown. If auto-start is true, the countdown will automatically start after reset--

Theme customization

Style variables

The component provides the following CSS variables, which can be used to customize styles. For usage, please refer to ConfigProvider component.

NameDefaultDescription
--z-count-down-text-colorvar(--z-text-color)-
--z-count-down-font-sizevar(--z-font-size-md)-
--z-count-down-line-heightvar(--z-line-height-md)-