function createTask(expression, func, immediateStart) {
var task = new Task(expression, func);
return new ScheduledTask(task, immediateStart);
}...
```
Import node-cron and schedule a task:
```javascript
var cron = require('node-cron');
cron.schedule('* * * * *', function(){
console.log('running a task every minute');
});
```
## Cron Syntax
This is a quick reference to cron syntax and also shows the options supported by node-cron.
...function ScheduledTask(task, immediateStart) {
this.task = function() {
task.update(new Date());
};
this.tick = null;
if (immediateStart !== false) {
this.start();
}
}n/a
function Task(pattern, execution){
validatePattern(pattern);
this.initialPattern = pattern.split(' ');
this.pattern = convertExpression(pattern);
this.execution = execution;
this.expressions = this.pattern.split(' ');
}n/a
function ScheduledTask(task, immediateStart) {
this.task = function() {
task.update(new Date());
};
this.tick = null;
if (immediateStart !== false) {
this.start();
}
}n/a
destroy = function () {
this.stop();
this.task = null;
}...
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('will not execute anymore, nor be able to restart');
});
task.destroy();
```
## Issues
Feel free to submit issues and enhancement requests [here](https://github.com/merencia/node-cron/issues).
## Contributors
...start = function () {
if (this.task && !this.tick) {
this.tick = setInterval(this.task, 1000);
}
return this;
}...
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('immediately started');
}, false);
task.start();
```
### Stop
The task won't be executed unless re-started.
```javascript
...stop = function () {
if (this.tick) {
clearInterval(this.tick);
this.tick = null;
}
return this;
}...
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('will execute every minute until stopped');
});
task.stop();
```
### Destroy
The task will be stopped and completely destroyed.
```javascript
...function Task(pattern, execution){
validatePattern(pattern);
this.initialPattern = pattern.split(' ');
this.pattern = convertExpression(pattern);
this.execution = execution;
this.expressions = this.pattern.split(' ');
}n/a
update = function (date){
if(mustRun(this, date)){
try {
this.execution();
} catch(err) {
console.error(err);
}
}
}...
* Creates a new scheduled task.
*
* @param {Task} task - task to schedule.
* @param {boolean} immediateStart - whether to start the task immediately.
*/
function ScheduledTask(task, immediateStart) {
this.task = function() {
task.update(new Date());
};
this.tick = null;
if (immediateStart !== false) {
this.start();
}
...