apps/mantine.dev/src/pages/hooks/use-interval.mdx
import { UseIntervalDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.useInterval);
To automatically start the interval when the component is mounted, set the autoInvoke option to true:
import { useInterval } from '@mantine/hooks';
const interval = useInterval(
() => console.log('Interval tick'),
1000,
{ autoInvoke: true }
);
import { useInterval } from '@mantine/hooks';
const { start, stop, toggle, active } = useInterval(fn, interval);
Arguments:
fn – function that is called at each interval tickinterval – amount of milliseconds between each tickReturn object:
start – start the intervalstop – stop intervaltoggle – toggle intervalactive – current interval statusinterface UseIntervalOptions {
/** If set, the interval will start automatically when the component is mounted, `false` by default */
autoInvoke?: boolean;
}
interface UseIntervalReturnValue {
/** Starts the interval */
start: () => void;
/** Stops the interval */
stop: () => void;
/** Toggles the interval */
toggle: () => void;
/** Indicates if the interval is active */
active: boolean;
}
function useInterval(
fn: () => void,
interval: number,
options?: UseIntervalOptions,
): UseIntervalReturnValue
UseIntervalOptions and UseIntervalReturnValue types are exported from the @mantine/hooks package;
you can import them in your application:
import type { UseIntervalOptions, UseIntervalReturnValue } from '@mantine/hooks';