Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useTimeout.js 652B

1234567891011121314151617181920212223242526
  1. import { useRef } from 'react';
  2. import useWillUnmount from './useWillUnmount';
  3. import useMounted from './useMounted';
  4. /**
  5. * Returns a controller object for setting a timeout that is properly cleaned up
  6. * once the component unmounts. New timeouts cancel and replace existing ones.
  7. */
  8. export default function useTimeout() {
  9. var isMounted = useMounted();
  10. var handle = useRef();
  11. var clear = function clear() {
  12. return clearTimeout(handle.current);
  13. };
  14. useWillUnmount(clear);
  15. return {
  16. set: function set(fn, ms) {
  17. if (!isMounted()) return;
  18. clear();
  19. handle.current = setTimeout(fn, ms);
  20. },
  21. clear: clear
  22. };
  23. }