Request Idle Callback
A ponyfill (opens in a new tab) for requestIdleCallback
, because Safari (a.k.a Modern IE) refuses to support it (opens in a new tab).
From MDN (opens in a new tab):
The
window.requestIdleCallback()
method queues a function to be called during a browser's idle periods. This enables developers to perform background and low-priority work on the main event loop, without impacting latency-critical events such as animation and input response.
Usage
import { useEffect } from 'react';
import { requestIdleCallback, cancelIdleCallback } from 'foxact/request-idle-callback';
const useIsIdle = () => {
const [isIdle, setIsIdle] = useState(false);
useEffect(() => {
const idleHandle = requestIdleCallback(() => {
setIsIdle(true);
});
return () => {
cancelIdleCallback(idleHandle);
}
});
return isIdle;
}