1.1.3 • Published 2 months ago

ctrl-timer v1.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

Enhanced and controllable setInterval、setTimeout in nodejs and browser

GitHub commit activity npm bundle size NPM Version NPM Type Definitions NPM Downloads Coverage Status

Features

  • Supporting pause and restart.
  • The design of API is close to native API.
  • Lightweight and does not rely on any third-party dependencies.
  • Can clear or pause all instances simultaneously.

Installation

npm i ctrl-timer

Usage

Interval

basic

import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
    console.log('hello-world!')
}, 1000)

options

import { Interval } from 'ctrl-timer'

const timer = new Interval({
    // Max number of exec, default infinity
    maxCount: 5,
    // After max, clear or pause, default clear
    maxClear: false,
})

timer.setInterval(() => {
    console.log('hello-world!')
}, 1000)

pause restart

import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
    console.log('hello-world!')
}, 1000)

timer.pause()
timer.restart()

update

import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
    console.log('hello-world!')
}, 1000)

timer.update({
    handler() {
        console.log('new handler!')
    },
    timeout: 3000
})

clearInterval

import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
    console.log('hello-world!')
}, 1000)

timer.clearInterval()
// not work
timer.pause()
// not work
timer.restart()

Timeout

import { Timeout } from 'ctrl-timer'

const timer = new Timeout()

timer.setTimeout(() => {
    console.log('hello-world!')
}, 1000)

Interval / Timeout static methods

import { Interval } from 'ctrl-timer'

const timer = new Timeout()

timer.setTimeout(() => {
    console.log('hello-world!')
}, 1000)

Interval.pause()
Interval.restart()
Interval.clearInterval()

Interval Instance API

methodsDescriptionTypeDefault
setInterval
pause
restart
updateupdate handler or timeout({handler, timeout}) => void
clearIntervalclear() => void

Timeout Instance API

methodsDescriptionTypeDefault
setTimeout
pause
restart
updateupdate handler or timeout({handler, timeout}) => void
clearIntervalclear() => void

Class static methods

methodsDescriptionTypeDefault
pausepause all() => void
restartrestart all() => void
clearTimeout (Timeout)clear all() => void
clearInterval (Interval)clear all() => void
1.1.3

2 months ago

1.1.1

2 months ago

1.1.0

2 months ago

1.0.0

2 months ago