useDelay
useDelay runs a body once, afterMs milliseconds after the component
appears, then stops.
useDelay(body: () => void, options: { afterMs: Int }): TimerReference
Section titled “Reference”useDelay(body, options)
Section titled “useDelay(body, options)”const Notice = (): Component => { const line = useRef();
useDelay(() => { line.hide = true; }, { afterMs: 2000 });
return ( <text ref={line} x={0} y="bottom" w="fill" h={16} font={Fonts.SMALL} text="Overload potion applied." align="center" valign="middle" shadow color={Colors.GREEN} /> );};Parameters
Section titled “Parameters”body: A function taking no arguments. Runs once.options.afterMs: A number of milliseconds to wait.
Returns
Section titled “Returns”A Timer with a stop() method, for cancelling before it elapses.
Caveats
Section titled “Caveats”- Refused in a component that reads state (
TS2E218). Move the delay into a child that does not read the changing value, or useuseIntervaland stop it yourself. - It uses the component’s one timer, like
useIntervalandonTimer. - A delay under a
hidedoes not fire until the component is shown. - Precision is about 20 milliseconds.
const Refused = (): Component => { const [n, setN] = useState(0); const line = useRef();
useDelay(() => { line.hide = true; }, { afterMs: 2000 }); // this component redrawsDismissing a notice
Section titled “Dismissing a notice” useDelay(() => { line.hide = true; }, { afterMs: 2000 });The countdown beside it reads state, so the delay lives in Notice:
<layer x={0} y={0} w="fill" h={40}> <SunkenBox> <layer x={2} y={2} w={{ fill: 4 }} h={{ fill: 4 }}> <BoostTimer /> </layer> </SunkenBox> </layer> <Notice />Troubleshooting
Section titled “Troubleshooting”The compiler refuses my delay
Section titled “The compiler refuses my delay”The component reads state. Split the delay into a child component that does not read that
value, as Notice is.
My delay never fires
Section titled “My delay never fires”Check the component is visible. A hidden component’s timers do not run, and resume when it comes back.