Skip to content
On this page

useTimestamp

Category
Export Size
1.23 kB
Last Changed
5 months ago

Reactive current timestamp

Demo

Timestamp: 1676716807224

Usage

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

const timestamp = useTimestamp({ offset: 0 })
import { useTimestamp } from '@vueuse/core'

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

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

html
<UseTimestamp v-slot="{ timestamp, pause, resume }">
  Current Time: {{ timestamp }}
  <button @click="pause()">Pause</button>
  <button @click="resume()">Resume</button>
</UseTimestamp>
<UseTimestamp v-slot="{ timestamp, pause, resume }">
  Current Time: {{ timestamp }}
  <button @click="pause()">Pause</button>
  <button @click="resume()">Resume</button>
</UseTimestamp>

Type Declarations

typescript
export interface UseTimestampOptions<Controls extends boolean> {
  /**
   * Expose more controls
   *
   * @default false
   */
  controls?: Controls
  /**
   * Offset value adding to the value
   *
   * @default 0
   */
  offset?: number
  /**
   * Update the timestamp immediately
   *
   * @default true
   */
  immediate?: boolean
  /**
   * Update interval, or use requestAnimationFrame
   *
   * @default requestAnimationFrame
   */
  interval?: "requestAnimationFrame" | number
  /**
   * Callback on each update
   */
  callback?: (timestamp: number) => void
}
/**
 * Reactive current timestamp.
 *
 * @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>
export interface UseTimestampOptions<Controls extends boolean> {
  /**
   * Expose more controls
   *
   * @default false
   */
  controls?: Controls
  /**
   * Offset value adding to the value
   *
   * @default 0
   */
  offset?: number
  /**
   * Update the timestamp immediately
   *
   * @default true
   */
  immediate?: boolean
  /**
   * Update interval, or use requestAnimationFrame
   *
   * @default requestAnimationFrame
   */
  interval?: "requestAnimationFrame" | number
  /**
   * Callback on each update
   */
  callback?: (timestamp: number) => void
}
/**
 * Reactive current timestamp.
 *
 * @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

Contributors

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

Changelog

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)
v8.9.3 on 7/14/2022
98985 - fix!: rename type TimestampOptions to UseTimestampOptions (#1892)

Released under the MIT License.