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.

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { useEffect } from 'react';
  2. import useCommittedRef from './useCommittedRef';
  3. /**
  4. * Creates a `setInterval` that is properly cleaned up when a component unmounted
  5. *
  6. * @param fn an function run on each interval
  7. * @param ms The milliseconds duration of the interval
  8. */
  9. function useInterval(fn, ms, paused) {
  10. if (paused === void 0) {
  11. paused = false;
  12. }
  13. var handle;
  14. var fnRef = useCommittedRef(fn); // this ref is necessary b/c useEffect will sometimes miss a paused toggle
  15. // orphaning a setTimeout chain in the aether, so relying on it's refresh logic is not reliable.
  16. var pausedRef = useCommittedRef(paused);
  17. var tick = function tick() {
  18. if (pausedRef.current) return;
  19. fnRef.current();
  20. schedule(); // eslint-disable-line no-use-before-define
  21. };
  22. var schedule = function schedule() {
  23. clearTimeout(handle);
  24. handle = setTimeout(tick, ms);
  25. };
  26. useEffect(function () {
  27. schedule();
  28. return function () {
  29. return clearTimeout(handle);
  30. };
  31. }, [paused]);
  32. }
  33. export default useInterval;