Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/zero-vue. Vue bindings for Zero
sync vue zero
Something went wrong. Try again.
564 B · 28 lines
TypeScript
1234567891011121314151617181920212223242526272829import type { MaybeRefOrGetter } from 'vue'import { onUnmounted, toValue, watch } from 'vue'
export function useInterval(callback: () => void, delay: MaybeRefOrGetter<number | null>) { let id: ReturnType<typeof setInterval> | undefined
function clear() { if (id) { clearInterval(id) id = undefined } }
const stop = watch(() => toValue(delay), (delay) => { clear()
if (delay === null) { return }
id = setInterval(callback, delay) }, { immediate: true })
onUnmounted(() => { stop() clear() })}