CINXE.COM

Croner - Overview

<!DOCTYPE html> <html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="color-scheme" content="light dark"> <link rel="canonical" href="https://croner.56k.guru/"> <title>Croner - Overview</title> <!-- Pico CSS --> <link rel="stylesheet" href="css/pico.min.css" inline=""> <!-- Font Awesome --> <link href="css/font-awesome.css" rel="stylesheet"> <!-- GitHub Dark Hightlight.js Theme --> <link href="css/github-dark.min.css" rel="stylesheet"> <!-- Lumocs CSS --> <link href="css/palette.css" rel="stylesheet" inline=""> <link href="css/general.css" rel="stylesheet" inline=""> <link href="css/sidebar.css" rel="stylesheet" inline=""> <link href="css/blockquotes.css" rel="stylesheet" inline=""> <!-- Lumocs JS --> <script src="js/theme-switcher.js" inline=""></script> <script src="js/hamburger.js" inline=""></script> <script src="js/hierarchy.js" inline=""></script> <!-- Overridable custom content --> <script async="" src="https://track.webpulseanalytics.com/client/653eb150048754e8b13f076e" type="module"></script> <link href="css/custom.css" rel="stylesheet"> <meta property="og:type" content="website"> <meta property="og:site_name" content="Croner"> <meta property="og:locale" content="en"> <meta property="og:title" content="Overview"> <meta property="og:description" content="Cron for JavaScript and TypeScript"> <meta property="og:url" content="https://croner.56k.guru/"> <meta name="twitter:card" content="summary"> <meta name="description" content="Cron for JavaScript and TypeScript"> </head> <body> <main> <!-- content --> <div class="grid"> <!-- Sidebar --> <aside> <div class="aside-content"> <header> <ul> <!-- Hamburger Icon --> <li id="hamburger-wrapper"> <div id="hamburger-btn"> <i class="fas fa-bars"></i> </div> </li> <!-- Brand Name --> <li> <a class="navbar-brand" href=""> Croner </a> </li> <!-- Light/Dark Theme Selector --> <li class="mobile right"><span class="theme-switcher"><i id="theme-icon" class="fas fa-moon"></i></span></li> <!-- Aux Top Links --> <li class="mobile right"> <a href="https://github.com/hexagon/croner" title="GitHub Repsitory"> <i class="fab fa-github"></i> </a> </li> <li class="mobile right"> <a href="https://deno.land/x/croner" title="Deno.land/x"> <i class="fas fa-cube"></i> </a> </li> <li class="mobile right"> <a href="https://npmjs.com/package/croner" title="NPM Library"> <i class="fab fa-npm"></i> </a> </li> </ul> </header> <section class="menu"> <nav> <ul> <!-- Page Hierarchy --> <li class="level0 bold "> <a href="">Overview</a> </li> <li class="level0 bold collapsed "> <a href="installation">Installation</a> </li> <li class="level0 bold collapsed children"> <div class="right collapser"> <i class="fas fa-chevron-down down"></i> <i class="fas fa-chevron-up up"></i> </div> <a href="usage">Usage</a> <ul> <li class="level1 collapsed "> <a href="usage/basics">Basics</a> </li> <li class="level1 collapsed "> <a href="usage/pattern">Pattern</a> </li> <li class="level1 collapsed "> <a href="usage/configuration">Configuration</a> </li> <li class="level1 collapsed "> <a href="usage/examples">Examples</a> </li> </ul> </li> <li class="level0 bold collapsed "> <a href="migration">Migration</a> </li> <li class="level0 bold collapsed "> <a href="contributing">Contributing</a> </li> <li class="level0 bold collapsed "> <a href="sponsors">Donations</a> </li> </ul> <!-- Add nav_links --> <ul> <li> <a href="https://github.com/hexagon/croner" title="GitHub Repsitory"> GitHub Repsitory <i class="fa fa-external-link" aria-hidden="true"></i> </a> </li> <li> <a href="https://deno.land/x/croner" title="Deno.land/x"> Deno.land/x <i class="fa fa-external-link" aria-hidden="true"></i> </a> </li> <li> <a href="https://npmjs.com/package/croner" title="NPM Library"> NPM Library <i class="fa fa-external-link" aria-hidden="true"></i> </a> </li> </ul> </nav> <!-- Sidebar custom content --> </section> </div> </aside> <section> <header> <ul> <!--<div id="search"></div>--> <li> <!-- Find the Current Page in Hierarchy --> <!-- Display the Breadcrumbs --> </li> <!-- Light/Dark Theme Selector --> <li class="right"><span class="theme-switcher"><i id="theme-icon" class="fas fa-moon"></i></span></li> <!-- Aux Top Links --> <li class="right"> <a href="https://github.com/hexagon/croner" title="GitHub Repsitory"> <i class="fab fa-github"></i> </a> </li> <li class="right"> <a href="https://deno.land/x/croner" title="Deno.land/x"> <i class="fas fa-cube"></i> </a> </li> <li class="right"> <a href="https://npmjs.com/package/croner" title="NPM Library"> <i class="fab fa-npm"></i> </a> </li> </ul> </header> <article> <p align="center"> <img class="logo" src="https://cdn.jsdelivr.net/gh/hexagon/croner@master/croner.png" alt="Croner" width="150" height="150"><br> Trigger functions or evaluate cron expressions in JavaScript or TypeScript. No dependencies. All features. Node. Deno. Bun. Browser. <br><br>Try it live on <a href="https://jsfiddle.net/hexag0n/hoa8kwsb/">jsfiddle</a>.<br> </p> <h2 id="features" tabindex="-1"><a href="#features" class="header-anchor">Features</a></h2> <ul> <li>Trigger functions in JavaScript using <a href="https://en.wikipedia.org/wiki/Cron#CRON_expression">Cron</a> syntax.</li> <li>Evaluate cron expressions and get a list of upcoming run times.</li> <li>Uses Vixie-cron <a href="usage/pattern">pattern</a>, with additional features such as <code>L</code> for last day and weekday of month and <code>#</code> for nth weekday of month.</li> <li>Works in Node.js &gt;=18.0 (both require and import), Deno &gt;=1.16 and Bun &gt;=1.0.0.</li> <li>Works in browsers as standalone, UMD or ES-module.</li> <li>Target different <a href="usage/examples/#time-zone">time zones</a>.</li> <li>Built-in <a href="usage/examples/#overrun-protection">overrun protection</a></li> <li>Built-in <a href="usage/examples/#error-handling">error handling</a></li> <li>Includes <a href="https://www.typescriptlang.org/">TypeScript</a> typings.</li> <li>Support for asynchronous functions.</li> <li>Pause, resume, or stop execution after a task is scheduled.</li> <li>Operates in-memory, with no need for a database or configuration files.</li> <li>Zero dependencies.</li> <li>Thoroughly tested and is relied upon by well-known projects such as <a href="https://github.com/unitech/pm2">pm2</a>, <a href="https://github.com/louislam/uptime-kuma">Uptime Kuma</a>, <a href="https://github.com/zwave-js/zwave-js-ui">ZWave JS</a> and <a href="https://github.com/truenas/webui">TrueNAS</a>.</li> </ul> <h2 id="quick-examples" tabindex="-1"><a href="#quick-examples" class="header-anchor">Quick examples</a></h2> <p><strong>Run a function at the interval defined by a cron expression</strong></p> <pre><code class="language-ts hljs language-typescript"><span class="hljs-keyword">const</span> job = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cron</span>(<span class="hljs-string">'*/5 * * * * *'</span>, <span class="hljs-function">() =&gt;</span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'This will run every five seconds'</span>); }); </code></pre> <p><strong>What dates do the next 100 sundays occur on?</strong></p> <pre><code class="language-ts hljs language-typescript"><span class="hljs-keyword">const</span> nextSundays = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cron</span>(<span class="hljs-string">'0 0 0 * * 7'</span>).<span class="hljs-title function_">nextRuns</span>(<span class="hljs-number">100</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(nextSundays); </code></pre> <p><strong>Days left to a specific date</strong></p> <pre><code class="language-ts hljs language-typescript"><span class="hljs-keyword">const</span> msLeft = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cron</span>(<span class="hljs-string">'59 59 23 24 DEC *'</span>).<span class="hljs-title function_">nextRun</span>() - <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>(msLeft/<span class="hljs-number">1000</span>/<span class="hljs-number">3600</span>/<span class="hljs-number">24</span>) + <span class="hljs-string">" days left to next christmas eve"</span>); </code></pre> <p><strong>Run a function at a specific date/time using a non-local timezone</strong></p> <p>Time is ISO 8601 local time, this will run 2024-01-23 00:00:00 according to the time in Asia/Kolkata</p> <pre><code class="language-ts hljs language-typescript"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Cron</span>(<span class="hljs-string">'2024-01-23T00:00:00'</span>, { <span class="hljs-attr">timezone</span>: <span class="hljs-string">'Asia/Kolkata'</span> }, <span class="hljs-function">() =&gt;</span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Yay!'</span>) }); </code></pre> <p>More examples at <a href="%5Busage/examples.md%5D">usage/examples.md</a></p> <h2 id="feature-comparison" tabindex="-1"><a href="#feature-comparison" class="header-anchor">Feature comparison</a></h2> <p>In this comparison, we outline the key differences between Croner and four other popular scheduling libraries: cronosjs, node-cron, cron, and node-schedule. The libraries are compared across various features such as platform compatibility, functionality, error handling, and Typescript support.</p> <p>The table below provides a brief overview of each library's features.</p> <table> <thead> <tr> <th></th> <th style="text-align:center">croner</th> <th style="text-align:center">cronosjs</th> <th style="text-align:center">node-cron</th> <th style="text-align:center">cron</th> <th style="text-align:center">node-schedule</th> </tr> </thead> <tbody> <tr> <td><strong>Platforms</strong></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td>Node.js (CommonJS)</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> </tr> <tr> <td>Browser (ESMCommonJS)</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td>Deno (ESM)</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td><strong>Features</strong></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td>Over-run protection</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td>Error handling</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center">✓</td> </tr> <tr> <td>Typescript typings</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> </tr> <tr> <td>Unref timers (optional)</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> </tr> <tr> <td>dom-OR-dow*</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> </tr> <tr> <td>dom-AND-dow* (optional)</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td>Next run</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> </tr> <tr> <td>Next n runs</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> </tr> <tr> <td>Timezone</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> </tr> <tr> <td>Minimum interval</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td>Controls (stop/resume)</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> </tr> <tr> <td>Range (0-13)</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> </tr> <tr> <td>Stepping (*/5)</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> </tr> <tr> <td>Last day of month (L)</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> <tr> <td>Nth weekday of month (#)</td> <td style="text-align:center">✓</td> <td style="text-align:center">✓</td> <td style="text-align:center"></td> <td style="text-align:center"></td> <td style="text-align:center"></td> </tr> </tbody> </table> <blockquote class="note"> <p><strong>DOM and DOW?</strong><br> DOM stands for Day of Month, and DOW stands for Day of Week.</p> </blockquote> </article> </section> </div> </main> <footer class="container-fluid"> <div class="container"> <div class="grid"> <!-- Custom footer content --> <span class="right notice">Powered by <a href="https://lumocs.56k.guru">Lumocs</a>, a documentation theme for <a href="https://lume.land">Lume</a>.</span> </div> </div> </footer> </body></html>

Pages: 1 2 3 4 5 6 7 8 9 10