Skip to content
On this page

useNow

Reactive custom timer with specified refresh rate

Parameters

typescript
import { useNow } from '@elonehoo/pistachio'

const now = useNow({
  refreshMs?: Number,
  sync?: Boolean,
  timeFn?: Function<Boolean>
})
import { useNow } from '@elonehoo/pistachio'

const now = useNow({
  refreshMs?: Number,
  sync?: Boolean,
  timeFn?: Function<Boolean>
})
ParametersTypeRequiredDefaultDescription
refreshMsNumberNo1000Refresh rate in milliseconds that the now gets updated
syncBooleanNotrueSync with the clock by the second
timeFnFunction<Boolean>NODate.nowFunction called when refresh the date

State

The useNow function exposes the following reactive state:

typescript
import { useNow } from '@elonehoo/pistachio'

const { now } = useNow()
import { useNow } from '@elonehoo/pistachio'

const { now } = useNow()
StateTypeDescription
nowRef<Number>Current time

Methods

The useNow function exposes the following methods:

typescript
import { useNow } from '@elonehoo/pistachio'

const { remove } = useNow()
import { useNow } from '@elonehoo/pistachio'

const { remove } = useNow()
SignatureDescription
removeManually stop the now to be refreshed

Example

Created 1 seconds ago

vue
<script setup lang="ts">
import { useNow } from '@elonehoo/pistachio'
let i = 0
const now = useNow({
  timeFn: () => ++i
})
</script>

<template>
  <div>
    <p>
      Created <b>{{ now.now }}</b> seconds ago
    </p>
  </div>
</template>
<script setup lang="ts">
import { useNow } from '@elonehoo/pistachio'
let i = 0
const now = useNow({
  timeFn: () => ++i
})
</script>

<template>
  <div>
    <p>
      Created <b>{{ now.now }}</b> seconds ago
    </p>
  </div>
</template>

Released under the MIT License.