useInterval

Declarative setInterval.

https://www.shaktools.com/shak-hooks
Utility

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useInterval } from "@shak-hooks/usehooks";
Source: packages/react/src/useInterval.ts

Signature

signature.ts
export function useInterval(callback: () => void, delay: number | null)

Parameters

NameTypeOptionalDefault
callback() => voidNo-
delaynumber | nullNo-

Returns

unknown

Implementation

use-interval.ts
1import { useEffect, useRef } from "react";
2
3export function useInterval(callback: () => void, delay: number | null) {
4 const savedCallback = useRef(callback);
5
6 useEffect(() => {
7 savedCallback.current = callback;
8 }, [callback]);
9
10 useEffect(() => {
11 if (delay !== null) {
12 const id = setInterval(() => savedCallback.current(), delay);
13 return () => clearInterval(id);
14 }
15 }, [delay]);
16}

Advertisement

Google Ads

Usage

example.tsx
1import { useInterval } from "@shak-hooks/usehooks";
2
3const result = useInterval(undefined, 0);
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy