Skip to content
On this page

useInterval

setInterval with start/remove and clearInterval on unmounted.

Parameters

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

useInterval(callback, ms?, ...args)
import { useInterval } from '@elonehoo/pistachio'

useInterval(callback, ms?, ...args)
ParametersTypeRequiredDefaultDescription
callback(...args):voidtruesetInterval callback
msnumber | false | undefinedfalseundefinedcallback interval ms, if ms provided it will setInterval automatically
...argsanyfalse[ ]callback args

Methods

The useInterval function exposes the following methods:

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

const { start, remove } = useInterval()
import { useInterval } from '@elonehoo/pistachio'

const { start, remove } = useInterval()
SignatureDescription
startStart
removeManually clearInterval

Example

vue
<script setup lang="ts">
import { ref } from 'vue'
import { useInterval } from '@elonehoo/pistachio'

const date = ref()

useInterval(() => date.value = new Date(), 1000)
</script>

<template>
  <div>
    {{ date }}
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useInterval } from '@elonehoo/pistachio'

const date = ref()

useInterval(() => date.value = new Date(), 1000)
</script>

<template>
  <div>
    {{ date }}
  </div>
</template>

Released under the MIT License.