setClockInterval
Execute a callback repeatedly at specified intervals, starting at the next clock second boundary
Usage
import { setClockInterval } from '@accelint/temporal';
const cleanup = setClockInterval(() => {
console.log('Tick:', new Date().toISOString());
}, 1000);
// Later, stop the interval
cleanup();Reference
function setClockInterval(
cb: () => void,
ms: number
): () => voidExecutes a callback repeatedly at a specified interval, aligned to clock second boundaries. Unlike standard setInterval, this function waits until the next clock second before starting, then automatically corrects for any execution drift on each iteration.
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | () => void | Callback function to execute at the next clock second, then repeatedly after each interval. |
ms | number | Time in milliseconds between callback executions. |
Returns
Returns a cleanup function that clears the interval when called. Safe to call multiple times.
Examples
Example: Updating a clock display
import { setClockInterval } from '@accelint/temporal';
function updateClock(element: HTMLElement) {
const cleanup = setClockInterval(() => {
element.textContent = new Date().toLocaleTimeString();
}, 1000);
return cleanup;
}
const cleanup = updateClock(document.getElementById('clock'));
// Updates every second, synchronized to system clockExample: Polling API with drift correction
import { setClockInterval } from '@accelint/temporal';
async function pollEndpoint(url: string, intervalMs: number) {
const cleanup = setClockInterval(async () => {
try {
const response = await fetch(url);
const data = await response.json();
console.log('Polled data:', data);
} catch (error) {
console.error('Poll failed:', error);
}
}, intervalMs);
return cleanup;
}
const cleanup = await pollEndpoint('/api/status', 5000);
// Polls every 5 seconds, correcting for any execution driftExample: Animation frame synchronization
import { setClockInterval } from '@accelint/temporal';
function animateCounter(element: HTMLElement, interval: number) {
let count = 0;
const cleanup = setClockInterval(() => {
count++;
element.textContent = String(count);
}, interval);
return cleanup;
}
const cleanup = animateCounter(document.getElementById('counter'), 250);
// Increments counter every 250ms, aligned to clock boundariesExample: Drift correction demonstration
import { setClockInterval } from '@accelint/temporal';
let lastTime = Date.now();
const cleanup = setClockInterval(() => {
const now = Date.now();
const elapsed = now - lastTime;
console.log(`Elapsed: ${elapsed}ms`);
lastTime = now;
}, 1000);
// Output stays consistent at ~1000ms despite callback execution time
// Standard setInterval would accumulate drift over timeGood to know: The first callback execution happens at the next clock second boundary, not immediately. For immediate execution, use standard
setInterval. The interval automatically corrects for drift by recalculating the delay on each iteration using internal timing utilities.
Related
- setClockTimeout - Execute callback once after delay
- callNextSecond - Execute callback at next clock second
- remainder - Calculate time until next interval boundary
Overview
@accelint/temporal provides temporal utilities for JavaScript applications. The library focuses on clock-aligned timing operations that synchronize with system clock boundaries, preventing drift in repeated timer operations.
setClockTimeout
Execute a callback once after a specified delay, starting from the next clock second boundary