1.0.2 • Published 9 months ago

@aakashx2838/use-timer v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Use Debounce

A simple React timer hook.

Installation

npm install @aakashx2838/use-timer

# or

yarn add @aakashx2838/use-timer

Usage

import { useTimer } from "./use-timer";

function App() {
  const { time, isRunning, toggle, reset } = useTimer({
    reverse: true,
    targetTime: -5,
    loop: true,
  });

  return (
    <>
      <p>
        {time.h}: {time.m}: {time.s}
      </p>
      <button onClick={toggle}>{isRunning ? "Stop" : "Start"}</button>
      <button onClick={reset}>Reset</button>
    </>
  );
}

export default App;

Defaults

let step = 1; // in seconds
let timeInterval = 1; // in seconds
let startTime = 0; // in seconds
let targetTime = 0; // in seconds
let reverse = false; // in seconds
let loop = false; // in seconds

let onComplete = () => {};
let onStart = () => {};
let onPause = () => {};
let onTimeUpdate = (time: number) => {};

Options

OptionTypeDescription
stepnumberThe amount of time to increment/decrement the timer by.
timeIntervalnumberThe interval at which the timer updates.
startTimenumberThe time at which the timer should start.
targetTimenumberThe time at which the timer should stop (Only provide if reverse is true).
reversebooleanIf true, the timer will count down. If false, the timer will count up.
loopbooleanIf true, the timer will restart when it reaches the target time.
onCompletefunctionA callback function that will be called when the timer reaches the target time.
onStartfunctionA callback function that will be called when the timer starts.
onPausefunctionA callback function that will be called when the timer is paused.
onTimeUpdatefunctionA callback function that will be called when the timer updates. The current time is passed in.

Structure of time

{
  h: number;
  m: number;
  s: number;
}
1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago