starlight-timer
v1.0.0
Published
A simple timer utility library for Starlight DSL
Maintainers
Readme
Starlight Timer Library
Author: Macedon
Package: starlight-timer
Introduction
The starlight-timer package provides utilities for working with timers in Starlight DSL.
It allows you to schedule tasks, pause execution, repeat actions, and measure execution time.
Importing the Package
You can import the library using:
import * as timer from "starlight-timer";
Functions
- timer.sleep(ms) - Pauses execution for
msmilliseconds. Example:
await timer.sleep(1000); // Waits 1 second
- timer.setTimeout(fn, ms) - Executes a function
fnonce aftermsmilliseconds.
timer.setTimeout(() => {
sldeploy("Timeout executed!");
}, 2000);
- timer.setInterval(fn, ms) - Executes a function
fnrepeatedly everymsmilliseconds.
let count = 0;
const intervalId = timer.setInterval(() => {
sldeploy("Interval running: " + count);
count++;
if (count >= 5) timer.clearInterval(intervalId); // Stop after 5 times
}, 1000);
- timer.clearInterval(id) - Stops an interval previously created by
timer.setInterval.
timer.measure(fn) - Measures the execution time of a synchronous or asynchronous function fn.
const duration = await timer.measure(async () => {
await timer.sleep(500);
});
sldeploy("Function took: " + duration + " ms");
Example Program
import * as timer from "starlight-timer";
sldeploy("Starting timer demo...");
// Sleep
await timer.sleep(1000);
sldeploy("Slept for 1 second");
// Timeout
timer.setTimeout(() => {
sldeploy("This runs after 2 seconds");
}, 2000);
// Interval
let count = 0;
const intervalId = timer.setInterval(() => {
sldeploy("Interval count: " + count);
count++;
if (count >= 3) timer.clearInterval(intervalId);
}, 1000);
// Measure execution
const duration = await timer.measure(async () => {
await timer.sleep(500);
});
sldeploy("Measured execution: " + duration + " ms");
Conclusion
The starlight-timer library is useful for asynchronous operations, scheduling, and performance measurement in Starlight DSL programs.
Use it to control timing, delay operations, or run repeated tasks.
