rong_timer
setTimeout、setInterval 与异步定时器
本页 API 参考由仓库内的英文文档生成,暂仅提供英文版。
Timer
Timer functions for callback-style scheduling.
Callback Style
setTimeout / clearTimeout
const id = setTimeout(() => {
console.log("runs after 1 second");
}, 1000);
clearTimeout(id); // cancel
setInterval / clearInterval
const id = setInterval(() => {
console.log("runs every second");
}, 1000);
clearInterval(id); // stop
Async / Sync Waiting
Rong.sleep(delay?)
Asynchronously waits for a number of milliseconds or until a target Date.
await Rong.sleep(100);
await Rong.sleep(new Date(Date.now() + 500));
Use this on hot runtime paths, in request handling, and anywhere blocking the current JS thread would be undesirable.
Rong.sleepSync(delay?)
Synchronously blocks the current JS thread for the given number of milliseconds.
Rong.sleepSync(25);
Use this only in tests, short scripts, startup-time work, or other paths where blocking is acceptable.