mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-02-26 18:09:03 +01:00
For now, this only works for unsent unsaved drafts e.g. the browser kill the page without giving the user the chance to discard
22 lines
513 B
JavaScript
22 lines
513 B
JavaScript
// useInterval with Preact
|
|
import { useEffect, useRef } from 'preact/hooks';
|
|
|
|
export default function useInterval(callback, delay) {
|
|
const savedCallback = useRef();
|
|
|
|
// Remember the latest callback.
|
|
useEffect(() => {
|
|
savedCallback.current = callback;
|
|
}, [callback]);
|
|
|
|
// Set up the interval.
|
|
useEffect(() => {
|
|
function tick() {
|
|
savedCallback.current();
|
|
}
|
|
if (delay !== null) {
|
|
let id = setInterval(tick, delay);
|
|
return () => clearInterval(id);
|
|
}
|
|
}, [delay]);
|
|
}
|