Skip to content

useTimestamp

Category
Export Size
817 B
Last Changed
15 minutes ago

响应式的当前时间戳

Demo

时间戳: 1730364080381

用法

js
import { useTimestamp } from '@vueuse/core'

const timestamp = useTimestamp({ offset: 0 })
js
const { timestamp, pause, resume } = useTimestamp({ controls: true })

组件用法

vue
<template>
  <UseTimestamp v-slot="{ timestamp, pause, resume }">
    当前时间:{{ timestamp }}
    <button @click="pause()">
      暂停
    </button>
    <button @click="resume()">
      恢复
    </button>
  </UseTimestamp>
</template>

类型声明

typescript
export interface UseTimestampOptions<Controls extends boolean> {
  /**
   * 暴露更多控制选项
   *
   * @default false
   */
  controls?: Controls
  /**
   * 添加到值的偏移量
   *
   * @default 0
   */
  offset?: number
  /**
   * 立即更新时间戳
   *
   * @default true
   */
  immediate?: boolean
  /**
   * 更新间隔,或使用 requestAnimationFrame
   *
   * @default requestAnimationFrame
   */
  interval?: "requestAnimationFrame" | number
  /**
   * 每次更新时的回调函数
   */
  callback?: (timestamp: number) => void
}
/**
 * 响应式的当前时间戳。
 *
 * @see https://vueuse.org/useTimestamp
 * @param options
 */
export declare function useTimestamp(
  options?: UseTimestampOptions<false>,
): Ref<number>
export declare function useTimestamp(options: UseTimestampOptions<true>): {
  timestamp: Ref<number>
} & Pausable
export type UseTimestampReturn = ReturnType<typeof useTimestamp>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
vaakian X
sun0day
Waleed Khaled
sun0day
Jelf
Scott Bedard
Shinigami
wheat
Alex Kozack
Antério Vieira

变更日志

v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)

Released under the MIT License.