JSPM

croner

1.1.31
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2420507
  • Score
    100M100P100Q194590F
  • License MIT

Isomorphic JavaScript cron parser and scheduler.

Package Exports

  • croner

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (croner) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Croner

Build status npm version Codacy Badge MIT License jsdelivr

Pure JavaScript minimal isomorphic cron parser and scheduler. Or simply speaking - setInterval on steroids.

Supports Node.js, requirejs, es-module and stand alone usage.

Documented with JSDoc for intellisense, and complete TypeScript typings for type checking.

<script src="https://cdn.jsdelivr.net/npm/croner@1.1.31/dist/croner.min.js"></script>
// Run a function each second
Cron('* * * * * *', function () {
    console.log('This will run every second');
});

Installation

Node.js

npm install croner

CDN

Copy and paste one of the following script tags.

Minified normal module (umd)

<script src="https://cdn.jsdelivr.net/npm/croner@1.1.31/dist/croner.min.js"></script>

Minified ESM-module

See usage-section for more examples, including ESM usage.

Manual (server/client)

  • Download latest zipball
  • Unpack
  • Grab croner.min.js or croner.min.mjs from the dist/ folder

Usage

Node.js/CommonJS

var Cron = require('croner');

Cron('* * * * * *', function () {
    console.log('This will run every second');
});

Browser ESM

Import croner.min.mjs from cdn or manually, see installation section above.

<script type="module">
import Cron from 'https://cdn.jsdelivr.net/npm/croner@1.1.31/dist/croner.min.mjs';

Cron('* * * * * *', () => {
    console.log('This will run every second');
});
</script>

Browser ESM with import-maps

Import croner.min.mjs from cdn or manually, see installation section above.

<script type="importmap">
{
  "imports": {
    "croner": "https://cdn.jsdelivr.net/npm/croner@1.1.31/dist/croner.min.mjs"
  }
}
</script>
<script type="module">
import Cron from 'croner';

Cron('* * * * * *', () => {
    console.log('This will run every second');
});
</script>

Browser, stand-alone

Include croner.min.js fron cdn or manually, see installation section above. Croner registers itself globally as 'Cron'.

// Run a function each second
Cron('* * * * * *', function () {
    console.log('This will run every second');
});

Browser, require.js

Include croner.min.js in your preferred way, it will register itself as a require.js module.

define(["croner"], function(Cron) {

    Cron('* * * * * *', function () {
        console.log('This will run every second');
    });

});

Examples

Minimalist scheduling

// Run a function each second
Cron('* * * * * *', function () {
    console.log('This will run every second');
});

Minimalist scheduling with stepping

// Run a function every fifth second
Cron('*/5 * * * * *', function () {
    console.log('This will run every fifth second');
});

Minimalist scheduling with range

// Run a function the first five seconds of a minute
Cron('0-4 * * * * *', function () {
    console.log('This will run the first five seconds every minute');
});

Minimalist scheduling with options

// Run a function each second, limit to five runs
Cron('* * * * * *', { maxRuns: 5 }, function () {
    console.log('This will run each second, but only five times.');
});

Minimalist scheduling with controls

// Run a function each second, get reference to job
var job = Cron('* * * * * *', function () {
    console.log('This will run each second.');
});

// Pause job
job.pause();

// Resume job
job.resume();

// Stop job
job.stop();

Basic scheduling

// Run every minute
var scheduler = Cron('0 * * * * *');

scheduler.schedule(function() {
    console.log('This will run every minute');
});

Scheduling with options

// Run every minute
var scheduler = Cron('0 * * * * *');

// Schedule with options (all options are optional)
scheduler.schedule({ maxRuns: 5 }, function() {
    console.log('This will run every minute.');
});

Scheduling with controls

// Run every minute
var scheduler = Cron('0 * * * * *');

// Schedule with options (all options are optional)
var job = scheduler.schedule({ maxRuns: 5 }, function() {
    console.log('This will run every minute.');
});

// Pause job
job.pause();

// Resume job
job.resume();

// Stop job
job.stop();

Full API

var o = Cron( <string pattern> [, <object options>] [, <function callback> ] );

// If Cron is initialized without a scheduled function, cron itself is returned
// and the following member functions is available.
o.next( [ <date previous> ] );
o.msToNext();
o.previous();

// If Cron is initialized _with_ a scheduled function, the job is retured instead.
// Otherwise you get a reference to the job when scheduling a new job.
var job = o.schedule( [ { startAt: <date>, stopAt: <date>, maxRuns: <integer> } ,] callback);

// These self-explanatory functions is available to control the job
job.pause();
job.resume();
job.stop();

Pattern

┌──────────────── second (0 - 59)
│ ┌────────────── minute (0 - 59)
│ │ ┌──────────── hour (0 - 23)
│ │ │ ┌────────── day of month (1 - 31)
│ │ │ │ ┌──────── month (1 - 12)
│ │ │ │ │ ┌────── day of week (0 - 6) 
│ │ │ │ │ │       (0 to 6 are Sunday to Saturday; 7 is Sunday, the same as 0)
│ │ │ │ │ │
* * * * * *

License

MIT