Skip to main content

useInterval

useInterval is a custom hook to execute a callback at a specified interval.

Usage

import { useState } from "react";
import useInterval from "./useInterval";

function TimerComponent() {
const [count, setCount] = useState(0);
const { clear } = useInterval(() => setCount((prev) => prev + 1), 1000);

return (
<div>
<p>Counter: {count}</p>
<button onClick={clear}>Stop Timer</button>
</div>
);
}

export default TimerComponent;

API Reference

Parameters

ParameterTypeDefaultDescription
callback() => void-Function to be executed at each interval.
intervalnumber1000Time in milliseconds between executions.

Behavior

  • Executes the callback function at the specified interval.
  • Returns a clear function to stop the interval manually.
  • Automatically clears the interval when the component unmounts.

Return Value

useInterval returns an object with the following method:

PropertyTypeDescription
clear() => voidStops the interval when called.