Skip to content

byronogis/pausable-timers

Repository files navigation

pausable-timers

npm version npm downloads bundle size codecov license

JSDocs

pausable-timers is a library for creating timers that can be paused and resumed.

Installation

# ✨ Auto-detect
npx nypm install pausable-timers

# npm
npm install pausable-timers

# yarn
yarn add pausable-timers

# pnpm
pnpm install pausable-timers

# bun
bun install pausable-timers

# deno
deno install pausable-timers

Basic Usage

import { pausableTimers } from 'pausable-timers'

const timer = pausableTimers({
  mode: 'interval', // 'timeout' | 'interval'
  args: [() => {
    /**
     * Execute after 1 second
     * 1s 后执行
     */
  }, 1000]
})

timer.pause()
timer.resume()
timer.clear()
timer.restart()
timer.isPaused()
timers.getRemainingTime()
timer.isCompleted() // only for timeout mode

Advanced Usage

Customize the timeout/interval power, like using worker-timers(A replacement for setInterval() and setTimeout() which works in unfocused windows.):

import { pausableTimers } from 'pausable-timers'
import { clearInterval, clearTimeout, setInterval, setTimeout } from 'worker-timers'

const timer = pausableTimers({
  setTimeout,
  setInterval,
  clearTimeout,
  clearInterval,
  args: [() => {
    /**
     * Execute after 1 second
     * 1s 后执行
     */
  }, 1000,]
})

Contribution

Local development
  • Clone this repository
  • Install the latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run tests using pnpm dev or pnpm test

Sponsors

License

Published under the MIT license. Made by @byronogis and community 💛


🤖 auto updated with automd (last updated: Fri Dec 06 2024)