Develop ultra fast with Prismane 🎉

useInterval

useInterval hook provides a simple way to handle complex intervals.

Import

import { useInterval } from "@prismane/core/hooks";

Usage

function Demo() {
  const [count, setCount] = useState(0);

  const increment = () => {
    setCount((prevCount) => prevCount + 1);
  };

  const { start, stop, toggle, active } = useInterval(increment, 1000);

  return (
    <Flex direction="column" gap={fr(2)}>
      <Text
        cl={(theme) => (theme.mode === "dark" ? ["base", 200] : ["base", 700])}
      >
        Counter: {count}
      </Text>
      <Button onClick={start}>Start</Button>
      <Button onClick={stop}>Stop</Button>
      <Button onClick={toggle}>Toggle</Button>
      <Text
        cl={(theme) => (theme.mode === "dark" ? ["base", 200] : ["base", 700])}
      >
        Active: {active.toString()}
      </Text>
    </Flex>
  );
}

API

Parameters

NameTypeDescriptionDefault
callback() => voidThe function that will be called on every iteration of the interval.-
delaynumberThe delay between interval iterations.-

Return Value

NameTypeDescription
start() => voidFunction that starts the interval.
stop() => voidFunction that stops the interval.
toggle() => voidFunction that toggles the interval.
activebooleanThe state of the interval.