Enhanced and controllable setInterval、setTimeout in nodejs and browser
- 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.
import { Interval } from 'ctrl-timer'
const timer = new Interval()
timer.setInterval(() => {
console.log('hello-world!')
}, 1000)
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)
import { Interval } from 'ctrl-timer'
const timer = new Interval()
timer.setInterval(() => {
console.log('hello-world!')
}, 1000)
timer.pause()
timer.restart()
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
})
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()
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()
methods |
Description |
Type |
Default |
setInterval |
|
|
|
pause |
|
|
|
restart |
|
|
|
update |
update handler or timeout |
({handler, timeout}) => void |
|
clearInterval |
clear |
() => void |
|
methods |
Description |
Type |
Default |
setTimeout |
|
|
|
pause |
|
|
|
restart |
|
|
|
update |
update handler or timeout |
({handler, timeout}) => void |
|
clearInterval |
clear |
() => void |
|
methods |
Description |
Type |
Default |
pause |
pause all |
() => void |
|
restart |
restart all |
() => void |
|
clearTimeout (Timeout) |
clear all |
() => void |
|
clearInterval (Interval) |
clear all |
() => void |
|