useInterval
Starts an interval when the component mounts, stops it when the component unmounts, and restarts it when the interval duration changes. Use it for polling, clocks, countdowns, or repeated UI updates.
Importing
ts
import { useInterval } from '@almighty-shogun/vue-utils'Usage
ts
import { ref } from 'vue'
import { useInterval } from '@almighty-shogun/vue-utils'
const count = ref(0);
const interval = useInterval(1000, () => count.value++);
interval.stop();Parameters
| Name | Type | Description |
|---|---|---|
ms | MaybeRef<number> | Interval duration. |
fn | Function | Function called by the interval. |
Returns
| Name | Type | Description |
|---|---|---|
start() | () => void | Starts the interval when it is not already running. |
stop() | () => void | Clears the active interval. |
Type signature
ts
declare function useInterval(
ms: MaybeRef<number>,
fn: Function
): UseInterval;
type UseInterval = {
start(): void;
stop(): void;
};