ctrl-timer
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

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

methods Description Type Default
setInterval
pause
restart
update update handler or timeout ({handler, timeout}) => void
clearInterval clear () => void

Timeout Instance API

methods Description Type Default
setTimeout
pause
restart
update update handler or timeout ({handler, timeout}) => void
clearInterval clear () => void

Class static methods

methods Description Type Default
pause pause all () => void
restart restart all () => void
clearTimeout (Timeout) clear all () => void
clearInterval (Interval) clear all () => void

Package Sidebar

Install

npm i ctrl-timer

Weekly Downloads

2

Version

1.1.3

License

ISC

Unpacked Size

47.6 kB

Total Files

19

Last publish

Collaborators

  • shisongyan