otskit / time
Creates a poller that repeatedly executes a function at specified intervals. Supports pausing when the document is hidden and handles both sync and async functions.
// Basic polling
const poller = createPoller(() => {
console.log('Polling...');
}, { interval: 1000 });
poller.start();
// Will log 'Polling...' every 1000ms
poller.stop();
console.log(poller.isRunning()); // false
// Async polling with pause when hidden
const apiPoller = createPoller(async () => {
const data = await fetch('/api/status');
return data.json();
}, {
interval: 5000,
pauseWhenHidden: true
});
apiPoller.start();Ignores repeated calls and only runs the function after a pause.
const debouncedFunc = debounce(() => {
console.log('Function called');
}, 1000, { immediate: true });
debouncedFunc(); // Function called immediately, then 1000ms laterPad a number to two digits with leading zero if needed.
const hour = padToTwoDigits(7);
console.log(hour); // '07'
// Format date components
const day = padToTwoDigits(5);
const month = padToTwoDigits(12);
const dateString = `${day}/${month}/2023`;
console.log(dateString); // '05/12/2023'Get the current timestamp in milliseconds.
timestamp(); // e.g. 1703123456789Convert minutes to hours and minutes.
toHoursAndMinutes(150); // { hours: 2, minutes: 30, formatted: '2hrs 30min' }