Dates, a monotonic clock, a scheduler and a throttle
Format dates, measure elapsed time, compute the next cron run, and throttle a button.
Apache-2.0Runs on this page
Package: @awacloud/fw
Format and shift a date, show elapsed time from a monotonic clock on a scheduled tick, compute the next cron firing, and throttle a busy button.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Pages that display and compute with dates in the reader's time zone, show a running duration, schedule periodic work in the tab, or protect a handler from click storms.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - The page stops its tick while the tab is hidden; stop it too when your
view goes away (
ticker.stop()).
What it shows
date.parseISO(strict, timezone required),date.formatwithIntloptions,date.add/startOf/diff.startOfand the cron work in local time, so the page shows local time too.scheduler.cron('0 9 * * 1').next(from)for the next firing, andscheduler.interval(fn, ms)withstart()/stop()for a tick, stopped onvisibilitychangewhile the tab is hidden.clock.monotonic()/since()/format()for elapsed time that never goes backwards.rateLimit.throttle(fn, ms): leading call at once, one trailing call for the burst, nothing in between.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { date } from '@awacloud/fw/io/time/date.js';
import { clock } from '@awacloud/fw/io/timing/clock.js';
import { scheduler } from '@awacloud/fw/io/timing/scheduler.js';
import { rateLimit } from '@awacloud/fw/io/timing/rateLimit.js';
fw.runtime.registerAllDeep([date, clock, scheduler, rateLimit]);
const DATE = fw.runtime.resolve('date');
const CLOCK = fw.runtime.resolve('clock');
const SCHEDULER = fw.runtime.resolve('scheduler');
const RATE = fw.runtime.resolve('rateLimit');
export const RELEASE = '2026-09-30T16:45:00Z';
// startOf and cron work in the local time zone, so the page shows local time
// too (bun test runs in UTC, which keeps the test expectations fixed).
const FORMAT = { locale: 'en-US', dateStyle: 'full', timeStyle: 'short' };
/**
* @param {string} iso an ISO 8601 instant with a timezone
* @returns {{ formatted: string, shifted: string, cron: string }}
*/
export function describe(iso) {
const instant = DATE.parseISO(iso);
if (!instant) throw new Error(`not a strict ISO 8601 date: ${iso}`);
const shifted = DATE.startOf(DATE.add(instant, 2, 'weeks'), 'days');
return {
formatted: DATE.format(instant, FORMAT),
shifted: `${DATE.format(shifted, FORMAT)} (${DATE.diff(shifted, instant, 'hours')} h later)`,
cron: DATE.format(SCHEDULER.cron('0 9 * * 1').next(instant), FORMAT),
};
}
/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
const info = describe(RELEASE);
show('formatted', info.formatted);
show('shifted', info.shifted);
show('cron', info.cron);
const opened = CLOCK.monotonic();
export const ticker = SCHEDULER.interval(() => show('elapsed', CLOCK.format(CLOCK.since(opened))), 250);
ticker.start();
// A hidden tab needs no tick: stop it, and resume when the tab is shown again.
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') ticker.stop();
else ticker.start();
});
let clicks = 0;
let saves = 0;
// At most one call per 300 ms: one at once, one trailing call for the burst.
const save = RATE.throttle(() => show('saves', String(++saves)), 300);
/** @type {HTMLElement} */ (document.getElementById('save')).addEventListener('click', () => {
show('clicks', String(++clicks));
save();
});index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Dates, a monotonic clock, a scheduler and a throttle</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Dates, a monotonic clock, a scheduler and a throttle</h1>
<dl>
<dt>Release date (your time zone)</dt><dd><output id="formatted"></output></dd>
<dt>Two weeks later, start of that day</dt><dd><output id="shifted"></output></dd>
<dt>Next Monday 09:00 after the release (local)</dt><dd><output id="cron"></output></dd>
<dt>Page open for</dt><dd><output id="elapsed">0 ms</output></dd>
</dl>
<button id="save" type="button">Save (throttled to one call per 300 ms)</button>
<p aria-live="polite">Clicks: <output id="clicks">0</output>, saves: <output id="saves">0</output></p>
</main>
</body>
</html>