Skip to content

useTimestamp

类别
导出体积
817 B
上次更改
2 minutes ago

响应式的当前时间戳

示例

时间戳: 1727403607234

用法

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>

源码

源码演示文档

贡献者

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)
v9.3.0 on 9/26/2022
33dbc - feat: optional callback for useInterval, useTimeout & useTimestamp (#2240)