Time formatting and control utility:
TimeFormat
allows handling time conversion, delays, loops, and queues of asynchronous tasks.
index.js
const { TimeFormat } = require("atosjs");
const t = new TimeFormat();
(async () => {
console.log(t.ms("1s")); // 1000
console.log(t.ms(1000)); // "1s"
t.time(1000, 3, (count) => {
console.log(`repeat ${count}.`);
});
t.time(t.ms("3.5s"), () => console.log("timeout."));
await t.sleep(t.ms("10s"));
console.log("10 seconds passed.");
const task1 = async () => {
console.log("Task 1 started");
await t.sleep(t.ms("1m"));
console.log("Task 1 finished");
};
const task2 = async () => {
console.log("Task 2 started");
await t.sleep(t.ms("5s"));
console.log("Task 2 finished");
};
t.queue([task1, task2])
.then(() => console.log("All tasks completed."))
.catch((error) => console.error("Error in tasks:", error));
})();
Expected result:
Terminal
1000
1s
repeat 1.
repeat 2.
repeat 3.
timeout.
10 seconds passed.
Task 1 started
Task 1 finished
Task 2 started
Task 2 finished
All tasks completed.
ms()
ms-example.js
const t = new TimeFormat();
console.log(t.ms("1s")); // 1000
console.log(t.ms(1000)); // "1s"
ms()
converts time values between string ("1s"
, "2.5m"
, "3h"
, etc.) and milliseconds.
t.ms("2.5s") → 2500
t.ms(60000) → "1m"
sleep()
sleep-example.js
const t = new TimeFormat();
(async () => {
console.log("Waiting...");
await t.sleep(t.ms("3s"));
console.log("3 seconds passed.");
})();
sleep(ms)
is an asynchronous delay. Ideal for timed pauses with await
.
time(ms, cb)
timeout-example.js
const t = new TimeFormat();
t.time(t.ms("3.5s"), () => {
console.log("Executed after 3.5 seconds.");
});
Executes once after the specified time (timeout).
time(ms, times, cb)
loop-time-example.js
const t = new TimeFormat();
t.time(1000, 3, (count) => {
console.log(`Repeat ${count}.`);
});
Executes the callback
multiple times at a fixed interval.
- The example above repeats 3 times every 1 second.
queue(tasks[])
queue-example.js
const t = new TimeFormat();
const task1 = async () => {
console.log("Task 1 started");
await t.sleep(t.ms("2s"));
console.log("Task 1 finished");
};
const task2 = async () => {
console.log("Task 2 started");
await t.sleep(t.ms("1s"));
console.log("Task 2 finished");
};
t.queue([task1, task2])
.then(() => console.log("All tasks completed"))
.catch(console.error);
Executes asynchronous functions one after another, even if they have different times.
🧩 Method Table
General summary of
new TimeFormat();
Method | Type | Example | Description |
---|---|---|---|
ms(value) | function | t.ms("1m") → 60000 | Converts string to milliseconds or vice versa. |
sleep(ms) | Promise | await t.sleep(1000) | Waits for the specified time in milliseconds. |
time(ms, cb) | void | t.time(3000, () => {}) | Executes a callback after a time (timeout). |
time(ms, times, cb) | void | t.time(1000, 3, (i) => {}) | Executes the callback in a loop, n times at each interval. |
queue(tasks[]) | Promise | await t.queue([...]) | Executes asynchronous functions in order (in queue). |
Last updated on: