Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

React timer hook

License

NotificationsYou must be signed in to change notification settings

amrlabib/react-timer-hook

Repository files navigation

React timer hook is a customreact hook, built to handle timer, stopwatch, and time logic/state in your react component.

  1. useTimer: Timers (countdown timer)
  2. useStopwatch: Stopwatch (count up timer)
  3. useTime: Time (return current time)

Setup

yarn add react-timer-hook ORnpm install --save react-timer-hook


useTimer -Demo

Example

importReactfrom'react';import{useTimer}from'react-timer-hook';functionMyTimer({ expiryTimestamp}){const{    totalSeconds,    milliseconds,    seconds,    minutes,    hours,    days,    isRunning,    start,    pause,    resume,    restart,}=useTimer({ expiryTimestamp,onExpire:()=>console.warn('onExpire called'),interval:20});return(<divstyle={{textAlign:'center'}}><h1>react-timer-hook</h1><p>Timer Demo</p><divstyle={{fontSize:'100px'}}><span>{days}</span>:<span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span>:<span>{milliseconds}</span></div><p>{isRunning ?'Running' :'Not running'}</p><buttononClick={start}>Start</button><buttononClick={pause}>Pause</button><buttononClick={resume}>Resume</button><buttononClick={()=>{// Restarts to 5 minutes timerconsttime=newDate();time.setSeconds(time.getSeconds()+300);restart(time)}}>Restart</button></div>);}exportdefaultfunctionApp(){consttime=newDate();time.setSeconds(time.getSeconds()+600);// 10 minutes timerreturn(<div><MyTimerexpiryTimestamp={time}/></div>);}

Settings

keyTypeRequiredDescription
expiryTimestampDate objectYESthis will define for how long the timer will be running
autoStartbooleanNoflag to decide if timer should start automatically, by default it is set totrue
intervalnumberNovalue to change the interval of the timer, by default it is set to 1000ms. Note: this value will not affect the timer, it will just define the frequency used to calculate the current timer values. For example, if you have a use case where milliseconds are used, you need to use a smaller value for the interval, for example, 20ms or 100ms based on your needs.
onExpireFunctionNocallback function to be executed once countdown timer is expired

Values

keyTypeDescription
millisecondsnumbermilliseconds value, to get accurate ms values you need to set interval to a smaller value example: 20ms
secondsnumberseconds value
minutesnumberminutes value
hoursnumberhours value
daysnumberdays value
totalSecondsnumbertotal number of seconds left in timer NOT converted to minutes, hours or days
totalMillisecondsnumbertotal number of milliseconds left in timer NOT converted to minutes, hours or days
isRunningbooleanflag to indicate if timer is running or not
pausefunctionfunction to be called to pause timer
startfunctionfunction if called after pause the timer will continue based on original expiryTimestamp
resumefunctionfunction if called after pause the timer will continue countdown from last paused state
restartfunctionfunction to restart timer with new expiryTimestamp, accept 2 arguments first is the newexpiryTimestamp of type Date object and second isautoStart of type boolean to decide if it should automatically start after restart or not, default istrue

useStopwatch -Demo

Example

importReactfrom'react';import{useStopwatch}from'react-timer-hook';functionMyStopwatch(){const{    totalSeconds,    milliseconds,    seconds,    minutes,    hours,    days,    isRunning,    start,    pause,    reset,}=useStopwatch({autoStart:true,interval:20});return(<divstyle={{textAlign:'center'}}><h1>react-timer-hook</h1><p>Stopwatch Demo</p><divstyle={{fontSize:'100px'}}><span>{days}</span>:<span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span>:<span>{milliseconds}</span></div><p>{isRunning ?'Running' :'Not running'}</p><buttononClick={start}>Start</button><buttononClick={pause}>Pause</button><buttononClick={reset}>Reset</button></div>);}exportdefaultfunctionApp(){return(<div><MyStopwatch/></div>);}

Settings

keyTypeRequiredDescription
autoStartbooleanNoif set totrue stopwatch will auto start, by default it is set tofalse
offsetTimestampDate objectNothis will define the initial stopwatch offset example:const stopwatchOffset = new Date(); stopwatchOffset.setSeconds(stopwatchOffset.getSeconds() + 300); this will result in a 5 minutes offset and stopwatch will start from 0:0:5:0 instead of 0:0:0:0
intervalnumberNovalue to change the interval of the stopwatch, by default it is set to 1000ms. Note: this value will not affect the stopwatch, it will just define the frequency used to calculate the current timer values. For example, if you have a use case where milliseconds are used, you need to use a smaller value for the interval, for example, 20ms or 100ms based on your needs.

Values

keyTypeDescription
millisecondsnumbermilliseconds value, to get accurate ms values you need to set interval to a smaller value example: 20ms
secondsnumberseconds value
minutesnumberminutes value
hoursnumberhours value
daysnumberdays value
totalSecondsnumbertotal number of seconds in stopwatch NOT converted to minutes, hours or days
isRunningbooleanflag to indicate if stopwatch is running or not
startfunctionfunction to be called to start/resume stopwatch
pausefunctionfunction to be called to pause stopwatch
resetfunctionfunction to be called to reset stopwatch to 0:0:0:0, you can also pass offset parameter to this function to reset stopwatch with offset, similar to howoffsetTimestamp will offset the initial stopwatch time, this function will accept also a second argument which will decide if stopwatch should automatically start after reset or not default istrue

useTime -Demo

Example

importReactfrom'react';import{useTime}from'react-timer-hook';functionMyTime(){const{    milliseconds,    seconds,    minutes,    hours,    ampm,}=useTime({format:'12-hour',interval:20});return(<divstyle={{textAlign:'center'}}><h1>react-timer-hook</h1><p>Current Time Demo</p><divstyle={{fontSize:'100px'}}><span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span><span>{milliseconds}</span><span>{ampm}</span></div></div>);}exportdefaultfunctionApp(){return(<div><MyTime/></div>);}

Settings

keyTypeRequiredDescription
formatstringNoif set to12-hour time will be formatted with am/pm
intervalnumberNovalue to change the interval of the time, by default it is set to 1000ms. Note: this value will not affect the thime, it will just define the frequency used to calculate the current time values. For example, if you have a use case where milliseconds are used, you need to use a smaller value for the interval, for example, 20ms or 100ms based on your needs.

Values

keyTypeDescription
millisecondsnumbermilliseconds value
secondsnumberseconds value
minutesnumberminutes value
hoursnumberhours value
ampmstringam/pm value if12-hour format is used

Deprecation Warning:

Starting fromv1.1.0 and above default exportuseTimer is deprecated, your old code will still work but it is better to start using named exports{ useTimer, useStopwatch, useTime }


[8]ページ先頭

©2009-2025 Movatter.jp