Skip to content

Instantly share code, notes, and snippets.

View GustavoHenriqueAP's full-sized avatar

Gustavo Henrique GustavoHenriqueAP

  • NetVMI do Brasil
  • 00:26 (UTC -12:00)
View GitHub Profile
@codeaid
codeaid / useTimeout.ts
Created November 10, 2020 20:03
React hook utilising setTimeout and exposing pause, resume and cancel callbacks
import { useCallback, useEffect, useRef } from 'react';
type UseTimeoutCallback = (...args: Array<any>) => void;
/**
* Set a timer, which executes a function once the timer expires. Exposes pause, resume and cancel
* callbacks to the consumers.
*
* @param {UseTimeoutCallback} fn Callback function to execute after the specified timeout
* @param {number} ms Timeout in milliseconds after which to execute the callback