Skip to content

Commit

Permalink
feat(useTimestamp): support interval option
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 5, 2021
1 parent 45fbe5a commit bc09bba
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/core/useTimestamp/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { timestamp } from '@vueuse/shared'
import { Pausable, timestamp, useIntervalFn } from '@vueuse/shared'
import { ref } from 'vue-demi'
import { useRafFn } from '../useRafFn'

Expand All @@ -9,6 +9,13 @@ export interface TimestampOptions {
* @default 0
*/
offset?: number

/**
* Update interval, or use requestAnimationFrame
*
* @default requestAnimationFrame
*/
interval?: 'requestAnimationFrame' | number
}

/**
Expand All @@ -18,14 +25,18 @@ export interface TimestampOptions {
* @param options
*/
export function useTimestamp(options: TimestampOptions = {}) {
const { offset = 0 } = options
const {
offset = 0,
interval = 'requestAnimationFrame',
} = options

const ts = ref(timestamp() + offset)

const controls = useRafFn(
() => ts.value = timestamp() + offset,
{ immediate: true },
)
const update = () => ts.value = timestamp() + offset

const controls: Pausable = interval === 'requestAnimationFrame'
? useRafFn(update, { immediate: true })
: useIntervalFn(update, interval, true)

return {
timestamp: ts,
Expand Down

0 comments on commit bc09bba

Please sign in to comment.