Skip to content

Instantly share code, notes, and snippets.

@dbk91
dbk91 / useSelfAdjustingInterval.ts
Last active November 26, 2022 03:29
Self adjusting interval timer based on Dan Abromov's useInterval implementation.
import * as React from "react";
export default function useSelfAdjustingInterval(callback, delay) {
const target = React.useRef(delay);
const savedCallback = React.useRef<() => void>();
const startTime = React.useRef<number | null>(null);
React.useEffect(() => {
savedCallback.current = callback;
}, [callback]);

Keybase proof

I hereby claim:

  • I am dbk91 on github.
  • I am dbk91 (https://keybase.io/dbk91) on keybase.
  • I have a public key ASBHfwVLtIUM4ReZc1kIrR5zTR46UpPS4SSVu362V1Ls1Qo

To claim this, I am signing this object: