Skip to content

useTimestamp

Category
Export Size
678 B
Last Changed
3 hours ago

响应式的当前时间戳

Demo

时间戳: 1744101206666

用法

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>

Type Declarations

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>,
): ShallowRef<number>
export declare function useTimestamp(options: UseTimestampOptions<true>): {
  timestamp: ShallowRef<number>
} & Pausable
export type UseTimestampReturn = ReturnType<typeof useTimestamp>

Source

SourceDemoDocs

Contributors

jorshen

Changelog

No recent changes

Released under the MIT License.