npm.io
0.0.13 • Published 5 years ago

@barelyreaper/background-timer

Licence
MIT
Version
0.0.13
Deps
0
Size
6 kB
Vulns
0
Weekly
0
Stars
4

background-timer

Simple react hook to run a timer in the background

Motivation

Nothing really, pulled it off a project I wrote it for and now it's a package.

Install

npm i @barelyreaper/background-timer

Usage

import { useBackgroundTimer, msTimestamp } from 'background-timer';

export default function App() {
  /**
   * @param milliseconds
   * @returns {timer: milliseconds, reset: function} resets the timer
   */

  const { timer, reset } = useBackgroundTimer(2 * 1000 * 60,{
    onTimerEnd:()=>{
      alert("Timer Ended");
    }
  });
  

  return <>
    <p>{msTimestamp(timer)}</p>
    <button disabled={timer>0} onClick={()=>reset()}>Reset Timer</button>
  </>;
}

Keywords