Skip to content

useTimer ​

time
test coverage
Last changed: 20 days ago

Hook that creates a timer functionality

typescript
import { useTimer } from '@siberiacancode/reactuse';

Usage ​

typescript
const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer();
// or
const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer(1000, () => console.log('ready'));
// or
const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer(1000);

Demo ​

Api ​

Parameters

NameTypeDefaultNote
secondsnumber-The seconds value that define for how long the timer will be running
callback() => void-The function to be executed once countdown timer is expired

Parameters

NameTypeDefaultNote
secondsnumber-The seconds value that define for how long the timer will be running
options.immediately?booleantrueThe flag to decide if timer should start automatically
options.onExpire?() => void-The function to be executed when the timer is expired
options.onTick?(timestamp: number) => void-The function to be executed on each tick of the timer

Source ​

Source • Demo

Contributors ​

Avatardebabin
Avatarkhmilevoi

Released under the MIT License.