npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

timer-man

v1.0.2

Published

A collection of useful time-related functions and a powerful Timer class

Downloads

104

Readme

timer-man

English | 中文

A collection of useful time-related functions and a powerful Timer class

Installation

npm i timer-man

Usage

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 is false. It will be called once even if maxTimes is set to 0
  • maxTimes: Maximum total number of calls, default is undefined meaning no limit. If callWhenStart is true, 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 is false
  • callWhenStart: Whether to call the function immediately when starting, default is false. It will be called once even if maxTimes is set to 0
  • maxTimes: Maximum total number of calls, default is undefined meaning no limit. If callWhenStart is true, 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 state
  • callTimes: Get the current number of calls
  • remainTimes: Get the remaining number of calls, returns Infinity if maxTimes is set to undefined
  • hasMoreTrigger: Get whether there are more triggers
  • events: Get the event listener
  • delay: Get or set the delay time
  • fn: Get or set the callback function
  • repeat: Get or set whether to trigger repeatedly
  • callWhenStart: Get or set whether to call the function immediately when starting
  • maxTimes: Get or set the maximum total number of calls

Methods:

  • start(): Start the timer
  • stop(): Stop the timer
  • reset(): Reset the timer
  • restart(): Restart the timer
  • waitTriggered(): Asynchronous function, wait for trigger and return the function call result
  • waitStop(): 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