timer-man
v1.0.2
Published
A collection of useful time-related functions and a powerful Timer class
Readme
timer-man
English | 中文
A collection of useful time-related functions and a powerful Timer class
Installation
npm i timer-manUsage
sleep: Asynchronous function that waits for a specified number of milliseconds
import { sleep } from "timer-man"
console.log('Start Sleep...')
await sleep(5000)
console.log('Awake')callSyncAfter, cancelCallSync: setTimeout and clearTimeout with time parameter first
import { callSyncAfter, cancelCallSync } from "timer-man"
const handle1 = callSyncAfter(100, () => { })
cancelCallSync(handle1)
callSyncAfter(100, () => console.log("callSyncAfter 100"))callAfter: Asynchronous function that calls a function after a specified delay and returns the result
import { callAfter } from "timer-man"
const result = await callAfter(1000, () => 'Call After 1000')
console.log(result)repeatEvery, cancelRepeat: Call a function at regular intervals
import { sleep, repeatEvery, cancelRepeat } from "timer-man"
const handle2 = repeatEvery(1000, () => 'Repeat Every 1000')
await sleep(3000)
cancelRepeat(handle2)
repeatEvery(200, () => { console.log('Repeat Every 200') }, { callWhenStart: true, maxTimes: 10 })The third parameter is optional:
callWhenStart: Whether to call the function immediately when starting, default isfalse. It will be called once even ifmaxTimesis set to 0maxTimes: Maximum total number of calls, default isundefinedmeaning no limit. IfcallWhenStartistrue, the initial call is also counted
loop: Call a function a specified number of times
import { loop } from "timer-man"
loop(10, () => console.log('Loop once'))Timer<T>: Timer class
The Timer class provides more flexible timer functionality, supporting single and repeated triggers, as well as various control options.
Examples
import { Timer } from "timer-man"
const timer = new Timer(1000, () => 'Timer Trigger Result')
const result = await timer.start().waitTriggered()
console.log(result)
await timer.start().waitStop()
console.log('Timer was stopped')import { Timer } from "timer-man"
const timer = new Timer(
1000,
() => {
const { callTimes, remainTimes, hasMoreTrigger } = timer
return { at: new Date(), callTimes, remainTimes, hasMoreTrigger }
},
{
repeat: true,
callWhenStart: true,
maxTimes: 5
}
)
timer.events.on('runningChanged', running => console.log('Running state changed to ', running))
timer.events.on('triggered', result => console.log('Triggered result:', result))
timer.start()Constructor:
new Timer(delay: number, fn?: () => T, options?: Partial<TimerOptions>)The third parameter is optional:
repeat: Whether to trigger repeatedly, default isfalsecallWhenStart: Whether to call the function immediately when starting, default isfalse. It will be called once even ifmaxTimesis set to 0maxTimes: Maximum total number of calls, default isundefinedmeaning no limit. IfcallWhenStartistrue, the initial call is also counted
You can modify the default configuration through Timer.defaultOptions, and Timer instances created afterwards will use the new default configuration
Properties:
running: Get the current running state or toggle the running statecallTimes: Get the current number of callsremainTimes: Get the remaining number of calls, returnsInfinityifmaxTimesis set toundefinedhasMoreTrigger: Get whether there are more triggersevents: Get the event listenerdelay: Get or set the delay timefn: Get or set the callback functionrepeat: Get or set whether to trigger repeatedlycallWhenStart: Get or set whether to call the function immediately when startingmaxTimes: Get or set the maximum total number of calls
Methods:
start(): Start the timerstop(): Stop the timerreset(): Reset the timerrestart(): Restart the timerwaitTriggered(): Asynchronous function, wait for trigger and return the function call resultwaitStop(): Asynchronous function, wait for stop
Notes
- All time units are in milliseconds
- The Timer object can be reused, provides more control options and event listening functionality, and is more suitable for complex timing requirements
