useIntervalWhen

Set an interval that can be paused.

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

API Reference

Installation

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

Signature

signature.ts
export function useIntervalWhen(callback: () => void, delay: number, when: boolean = true, immediate: boolean = false)

Parameters

NameTypeOptionalDefault
callback() => voidNo-
delaynumberNo-
whenbooleanYestrue
immediatebooleanYesfalse

Returns

unknown

Implementation

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

Advertisement

Google Ads

Usage

example.tsx
1import { useIntervalWhen } from "@shak-hooks/usehooks";
2
3const result = useIntervalWhen(undefined, 0, undefined, undefined);
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