Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

586 B
Raw Blame
import { useEffect, useRef } from 'react'

import { usePageVisibility } from './usePageVisibility'

export function useInterval(callback: () => void, delay: number | null): void {
    const savedCallback = useRef(callback)

    useEffect(() => {
        savedCallback.current = callback
    }, [callback])
    const { isVisible } = usePageVisibility()

    useEffect(() => {
        if (delay === null || !isVisible) {
            return
        }

        const id = setInterval(() => savedCallback.current(), delay)
        return () => clearInterval(id)
    }, [delay, isVisible])
}