Skip to content

m-countdown 倒计时 ​

简述 ​

倒计时组件,用于获取验证码、活动倒计时等。

注意

请以 uni_modules/m-unix 与本文为准;各端差异以 uni-app 与各平台官方文档为准。涉及隐私能力(相册、定位、剪贴板、手机号等)需在 manifest 与后台完成配置。

平台差异说明 ​

App(vue)App(nvue)H5小程序
√√√√

演示地址 ​

与线上 H5 演示基座 分包一致(文档站右下角预览 iframe 亦指向同一路径)。

类型地址
分包路径pages_demo/countdown/countdown
线上 H5(hash)打开演示

基本使用 ​

uvue
<template>
	<view>
		<m-countdown
			:time="time"
			:format="HH:mm:ss"
			@finish="handleFinish"
		/>
	</view>
</template>

Props ​

参数说明类型默认值
time倒计时秒数number60
seconds与 time 同义;大于 0 时优先于 time(便于与演示写法一致)number0
format格式化字符串 DD 天 HH 小时 mm 分钟 ss 秒(非短信文案模式时)stringHH:mm:ss
autoStart是否自动开始booleantrue
typebutton 时为带边框底色的块状样式,否则为纯文字string''
suffix短信倒计时:进行中显示为「剩余秒数 + suffix」,如 秒后重试string''
endText短信倒计时:结束后显示;默认可点击重新开始(见 restartOnTap)string''
startTextautoStart 为 false 且未调用 start 时显示的文案string''
restartOnTap已结束且设置了 endText 时,点击是否重新倒计时booleantrue

Events ​

事件名说明
change倒计时变化时触发,参数含 day、hour、minute、second
finish倒计时结束时触发
tap组件被点击时触发

Methods ​

方法名说明
start开始倒计时
pause暂停倒计时
reset重置倒计时

示例 ​

验证码倒计时 ​

uvue
<template>
	<m-button
		v-if="countdown <= 0"
		type="primary"
		text="获取验证码"
		@click="sendCode"
	></m-button>
	<m-button
		v-else
		disabled
		text="{{ countdown }}s后重新获取"
	></m-button>
</template>

<script setup lang="uts">
	const countdown = ref(0)
	const countdownRef = ref(null)

	const sendCode = () => {
		// 发送验证码逻辑
		countdown.value = 60
		countdownRef.value.start()
	}

	const handleFinish = () => {
		countdown.value = 0
	}
</script>

Released under the MIT License.