Skip to content
On this page

Stopwatch 秒表

注:

  • 本组件没有指定样式,默认情况下可对文字样式进行自定义。
vue
<template>
  <ta-group title="基础用法">
    <div class="exp-stopwatch-box">
      <div class="exp-stopwatch-box-header">
        <ta-stopwatch
          @stop="onStop"
          @start="onStart"
          @reset="onReset"
          ref="stopWatch"
        ></ta-stopwatch>
      </div>
      <div class="exp-stopwatch-box-body">
        <ta-button @click="resetOrLap">
          {{ paused ? '重置' : '计次' }}
        </ta-button>
        <ta-button @click="startOrStop" :type="!paused ? 'danger' : 'success'">
          {{ paused ? '启动' : '停止' }}
        </ta-button>
      </div>
    </div>
    <ta-cell :label="'计次 ' + (laps.length - index)" v-for="(item, index) in laps" :key="item">
      {{ item }}
    </ta-cell>
  </ta-group>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import type { CountTime, TaStopwatch, StopwatchOnStop } from '@/index'

export default defineComponent({
  name: 'ExpStopwatch',
  setup() {
    const paused = ref(true)
    const laps = ref<string[]>([])
    const stopWatch = ref<InstanceType<typeof TaStopwatch>>()

    const setLaps = (_laps: CountTime[] = []) => {
      laps.value = _laps.reverse().map(countTime => {
        return `${parseInt(countTime.fullHours) > 0 ? countTime.thousandsFullHours + ':' : ''}${
          countTime.minutes
        }:${countTime.seconds}.${countTime.milliseconds}`
      })
    }

    const resetOrLap = () => {
      if (paused.value) {
        stopWatch.value?.reset()
        laps.value = []
      } else {
        setLaps(stopWatch.value?.lap())
      }
    }

    const startOrStop = () => {
      if (paused.value) {
        stopWatch.value?.start()
      } else {
        stopWatch.value?.stop()
      }
    }

    const onStop: StopwatchOnStop = e => {
      paused.value = true

      console.log('stop', e)

      setLaps(e.laps)
    }

    const onStart = () => {
      paused.value = false
    }

    const onReset = () => {
      paused.value = true
    }

    return {
      paused,
      laps,
      stopWatch,

      resetOrLap,
      startOrStop,
      onStart,
      onReset,
      onStop
    }
  }
})
</script>

Import

js
import { TaStopwatch } from 'tantalum-ui-mobile'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

ts
import type { CountTime, StopwatchOnStop } from 'tantalum-ui-mobile'

Props

属性类型默认值必填说明
show-millisecondsbooleantrue是否显示毫秒数
thousandsbooleantrue小时位是否以千分位形式显示

Events

事件描述回调函数参数函数 TypeScript
start计时启动时触发
stop计时停止时触发payload: { detail: CountTime, laps: CountTime[] } detail 周期总时间,laps,周期内所有计次时间StopwatchOnStop
reset计时复位时触发

CountTime 的结构

字段名类型说明
timenumber持续时间
daysstring天数
hoursstring小时数(<24),需要跟 days 配合,保留 2 位
fullHoursstring小时数,含天数综合,如 '124',至少保留 2 位
thousandsFullHoursstring千分位形式的小时数,含天数综合,如 '1,234'
minutesstring分钟数,保留 2 位
secondsstring秒钟数,保留 2 位
millisecondsstring毫秒数,保留 3 位

Methods

方法名说明参数
start开始计时() => void
stop停止计时() => void
reset复位/重置() => void
lap触发 1 次计次,返回一个周期() => CountTime[]

Slots

自定义风格显示(#default)

vue
<ta-stopwatch>
  <template #default="countTime">
    {{ countTime.fullHours }}:{{ countTime.minutes }}:{{
        countTime.seconds
    }}.{{ countTime.milliseconds }}
  </template>
</ta-stopwatch>