https://github.com/yss/crontabjs
For manage setTimeout and setInterval
https://github.com/yss/crontabjs
Last synced: 3 months ago
JSON representation
For manage setTimeout and setInterval
- Host: GitHub
- URL: https://github.com/yss/crontabjs
- Owner: yss
- Created: 2014-05-24T01:52:37.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-06-11T08:52:02.000Z (almost 8 years ago)
- Last Synced: 2025-02-18T04:37:22.014Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Crontab.js
the manager of setInterval and setTimeout.
## The core of the idea
Maintaining a queue, iterate over all those queue and execute it if needed when every time start.
## Install
`npm install crontabjs`
## Implementation
### Class
Crontab(interval, tolerance)
* `interval` the interval time of crontab run. unit is ms, default is 400ms.
* `tolerance` the maximum error of current time and last execute time. if `currTime + tolerance - lastExecuteTime - intervalTime` greater than 0, then this item will be run.#### getInstance()
Also, you can use `var crontab = Crontab.getInstance();` to get the only one instance.
This is for global invoke. return the same instance of Crontab. If no instance, create a new one.
### Method
#### on(fn, time[, context])
register a function. same as `setInterval`.
* `time` means the interval of function run. the unit is `ms`
* `fn` means the function which wait for run.
* `context` means the context of fn.#### one(fn, time[, context])
like `on`, register once. same as `setTimeout`.
#### off([id])
clear a item which id is `id` in queue. if `id` not set, clear all queue.
#### start
start run crontab. this will be auto run when use `new Crontab()`.
#### stop
stop run crontab.
#### setInterval(fn, time[, context])
alias `on(fn, time[, context])`.
#### setTimeout(fn, time[, context])
alias `one(fn, time[, context])`.
#### clearTimeout(id)
alias `off(id)`.
#### clearInterval(id)
alias `off(id)`.
#### setIntervalTime
set the interval time of the current instance of crontab.
this will run `stop` and `start` method in turn when it invoked.
#### setToleranceTime
set the tolerance time of the current instance of crontab.
## Test
```js
# test in browser
npm run-script browser# test in terminal
mocha
# or
npm run-script test```
## LICENSE
Apache License 2.0