GithubHelp home page GithubHelp logo

aksjer / use-timer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thibaultboursier/use-timer

0.0 2.0 0.0 71 KB

A timer hook for React

License: MIT License

JavaScript 6.45% TypeScript 93.55%

use-timer's Introduction

use-timer

npm Version License Linux Build Status Bundle size Bundle size

Simple timer turned into React Hooks. Read about Hooks feature.

Installation

npm i use-timer --save

Examples

Try demo here: https://stackblitz.com/edit/use-timer.

Usage

Basic timer

import React from 'react';
import { useTimer } from 'use-timer';

const App = () => {
  const { time, start, pause, reset } = useTimer();

  return (
    <React.Fragment>
      <div>
        <button onClick={start}>Start</button>
        <button onClick={pause}>Pause</button>
        <button onClick={reset}>Reset</button>
      </div>
      <p>Elapsed time: {time}</p>
    </React.Fragment>
  );
};

Decremental timer

import React from 'react';
import { useTimer } from 'use-timer';

const App = () => {
  const { time, start, pause, reset } = useTimer({
    initialTime: 100,
    timerType: 'DECREMENTAL',
  });

  return (
    <React.Fragment>
      <div>
        <button onClick={start}>Start</button>
        <button onClick={pause}>Pause</button>
        <button onClick={reset}>Reset</button>
      </div>
      <p>Remaining time: {time}</p>
    </React.Fragment>
  );
};

Timer with end time

import React from 'react';
import { useTimer } from 'use-timer';

const App = () => {
  const { time, start, pause, reset } = useTimer({
    endTime: 30,
    initialTime: 10,
  });

  return (
    <React.Fragment>
      <div>
        <button onClick={start}>Start</button>
        <button onClick={pause}>Pause</button>
        <button onClick={reset}>Reset</button>
      </div>
      <p>Ellapsed time: {time}</p>
    </React.Fragment>
  );
};

Configuration

The configuration and all its parameters are optional.

Property Type Default value Description
endTime number null the value for which timer stops
initialTime number 0 the starting value for the timer
interval number 1000 the interval in milliseconds
timerType string "INCREMENTAL" the choice between a value that increases ("INCREMENTAL") or decreases ("DECREMENTAL")

use-timer's People

Contributors

thibaultboursier avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.