@ricsam/quickjs-timers
v0.2.17
Published
Timer APIs (setTimeout, setInterval, clearTimeout, clearInterval) for QuickJS runtime
Maintainers
Readme
@ricsam/quickjs-timers
Timer APIs (setTimeout, setInterval, clearTimeout, clearInterval) for QuickJS runtime.
Note: This is a low-level package. For most use cases, use
@ricsam/quickjs-runtimewithcreateRuntime()instead.
Installation
bun add @ricsam/quickjs-timersSetup
import { setupTimers } from "@ricsam/quickjs-timers";
const handle = setupTimers(context);
// Clear all pending timers
handle.clearAll();
// Cleanup
handle.dispose();Injected Globals
setTimeout(callback, delay, ...args)- Execute callback after delayclearTimeout(id)- Cancel a timeoutsetInterval(callback, delay, ...args)- Execute callback repeatedlyclearInterval(id)- Cancel an interval
Usage in QuickJS
// setTimeout
const timeoutId = setTimeout(() => {
console.log("Executed after 1 second");
}, 1000);
// clearTimeout
clearTimeout(timeoutId);
// setInterval
let count = 0;
const intervalId = setInterval(() => {
count++;
console.log(`Tick ${count}`);
if (count >= 5) {
clearInterval(intervalId);
}
}, 100);
// With arguments
setTimeout((a, b) => {
console.log(a + b); // 3
}, 100, 1, 2);Host Integration
Timers integrate with QuickJS's job queue. The host must call runtime.executePendingJobs() to process timer callbacks:
// After setting up timers and evaluating code
while (runtime.hasPendingJob()) {
const result = runtime.executePendingJobs();
if (result.error) {
console.error("Timer error:", context.dump(result.error));
result.error.dispose();
}
}