ctrl-timer
v1.1.3
Published
Enhanced and controllable setInterval、setTimeout
Downloads
17
Maintainers
Readme
Enhanced and controllable setInterval、setTimeout in nodejs and browser
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 | |