2026-01-24 22:40:22 +00:00
<!DOCTYPE html>
<!-- retoor <retoor@molodetz.nl> -->
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title > timer - Wren-CLI Manual</ title >
< link rel = "stylesheet" href = "../css/style.css" >
</ head >
< body >
< button class = "mobile-menu-toggle" > Menu</ button >
< div class = "container" >
< aside class = "sidebar" >
< div class = "sidebar-header" >
< h1 >< a href = "../index.html" > Wren-CLI</ a ></ h1 >
< div class = "version" > v0.4.0</ div >
</ div >
< nav class = "sidebar-nav" >
< div class = "section" >
< span class = "section-title" > Getting Started</ span >
< ul >
< li >< a href = "../getting-started/index.html" > Overview</ a ></ li >
< li >< a href = "../getting-started/installation.html" > Installation</ a ></ li >
< li >< a href = "../getting-started/first-script.html" > First Script</ a ></ li >
< li >< a href = "../getting-started/repl.html" > Using the REPL</ a ></ li >
</ ul >
</ div >
< div class = "section" >
< span class = "section-title" > Language</ span >
< ul >
< li >< a href = "../language/index.html" > Syntax Overview</ a ></ li >
< li >< a href = "../language/classes.html" > Classes</ a ></ li >
< li >< a href = "../language/methods.html" > Methods</ a ></ li >
< li >< a href = "../language/control-flow.html" > Control Flow</ a ></ li >
< li >< a href = "../language/fibers.html" > Fibers</ a ></ li >
< li >< a href = "../language/modules.html" > Modules</ a ></ li >
</ ul >
</ div >
< div class = "section" >
< span class = "section-title" > API Reference</ span >
< ul >
< li >< a href = "index.html" > Overview</ a ></ li >
2026-01-25 03:58:39 +00:00
< li >< a href = "string.html" > String</ a ></ li >
< li >< a href = "number.html" > Num</ a ></ li >
2026-01-25 12:09:28 +00:00
< li >< a href = "argparse.html" > argparse</ a ></ li >
2026-01-24 22:40:22 +00:00
< li >< a href = "base64.html" > base64</ a ></ li >
< li >< a href = "crypto.html" > crypto</ a ></ li >
2026-01-25 12:09:28 +00:00
< li >< a href = "dataset.html" > dataset</ a ></ li >
2026-01-24 22:40:22 +00:00
< li >< a href = "datetime.html" > datetime</ a ></ li >
2026-01-25 12:09:28 +00:00
< li >< a href = "dns.html" > dns</ a ></ li >
< li >< a href = "env.html" > env</ a ></ li >
< li >< a href = "fswatch.html" > fswatch</ a ></ li >
< li >< a href = "html.html" > html</ a ></ li >
< li >< a href = "http.html" > http</ a ></ li >
2026-01-24 22:40:22 +00:00
< li >< a href = "io.html" > io</ a ></ li >
2026-01-25 12:09:28 +00:00
< li >< a href = "jinja.html" > jinja</ a ></ li >
< li >< a href = "json.html" > json</ a ></ li >
< li >< a href = "markdown.html" > markdown</ a ></ li >
2026-01-24 22:40:22 +00:00
< li >< a href = "math.html" > math</ a ></ li >
2026-01-25 12:09:28 +00:00
< li >< a href = "net.html" > net</ a ></ li >
< li >< a href = "os.html" > os</ a ></ li >
< li >< a href = "pathlib.html" > pathlib</ a ></ li >
< li >< a href = "regex.html" > regex</ a ></ li >
< li >< a href = "scheduler.html" > scheduler</ a ></ li >
< li >< a href = "signal.html" > signal</ a ></ li >
< li >< a href = "sqlite.html" > sqlite</ a ></ li >
< li >< a href = "subprocess.html" > subprocess</ a ></ li >
< li >< a href = "sysinfo.html" > sysinfo</ a ></ li >
< li >< a href = "tempfile.html" > tempfile</ a ></ li >
< li >< a href = "timer.html" class = "active" > timer</ a ></ li >
< li >< a href = "tls.html" > tls</ a ></ li >
< li >< a href = "udp.html" > udp</ a ></ li >
< li >< a href = "uuid.html" > uuid</ a ></ li >
< li >< a href = "wdantic.html" > wdantic</ a ></ li >
< li >< a href = "web.html" > web</ a ></ li >
< li >< a href = "websocket.html" > websocket</ a ></ li >
2026-01-24 22:40:22 +00:00
</ ul >
</ div >
< div class = "section" >
< span class = "section-title" > Tutorials</ span >
< ul >
< li >< a href = "../tutorials/index.html" > Tutorial List</ a ></ li >
2026-01-25 12:09:28 +00:00
< li >< a href = "../tutorials/http-client.html" > HTTP Client</ a ></ li >
< li >< a href = "../tutorials/websocket-chat.html" > WebSocket Chat</ a ></ li >
< li >< a href = "../tutorials/database-app.html" > Database App</ a ></ li >
< li >< a href = "../tutorials/template-rendering.html" > Templates</ a ></ li >
< li >< a href = "../tutorials/cli-tool.html" > CLI Tool</ a ></ li >
2026-01-25 18:02:02 +00:00
< li >< a href = "../tutorials/web-server.html" > Web Server</ a ></ li >
2026-01-24 22:40:22 +00:00
</ ul >
</ div >
< div class = "section" >
< span class = "section-title" > How-To Guides</ span >
< ul >
< li >< a href = "../howto/index.html" > How-To List</ a ></ li >
2026-01-25 12:09:28 +00:00
< li >< a href = "../howto/http-requests.html" > HTTP Requests</ a ></ li >
< li >< a href = "../howto/json-parsing.html" > JSON Parsing</ a ></ li >
< li >< a href = "../howto/regex-patterns.html" > Regex Patterns</ a ></ li >
< li >< a href = "../howto/file-operations.html" > File Operations</ a ></ li >
< li >< a href = "../howto/async-operations.html" > Async Operations</ a ></ li >
< li >< a href = "../howto/error-handling.html" > Error Handling</ a ></ li >
2026-01-24 22:40:22 +00:00
</ ul >
</ div >
2026-01-25 18:02:02 +00:00
< div class = "section" >
< span class = "section-title" > Contributing</ span >
< ul >
< li >< a href = "../contributing/index.html" > Overview</ a ></ li >
< li >< a href = "../contributing/module-overview.html" > Module Architecture</ a ></ li >
< li >< a href = "../contributing/pure-wren-module.html" > Pure-Wren Modules</ a ></ li >
< li >< a href = "../contributing/c-backed-module.html" > C-Backed Modules</ a ></ li >
< li >< a href = "../contributing/foreign-classes.html" > Foreign Classes</ a ></ li >
< li >< a href = "../contributing/async-patterns.html" > Async Patterns</ a ></ li >
< li >< a href = "../contributing/testing.html" > Writing Tests</ a ></ li >
< li >< a href = "../contributing/documentation.html" > Documentation</ a ></ li >
</ ul >
</ div >
2026-01-24 22:40:22 +00:00
</ nav >
</ aside >
< main class = "content" >
< nav class = "breadcrumb" >
< a href = "../index.html" > Home</ a >
< span class = "separator" > /</ span >
< a href = "index.html" > API Reference</ a >
< span class = "separator" > /</ span >
< span > timer</ span >
</ nav >
< article >
< h1 > timer</ h1 >
2026-01-25 12:09:28 +00:00
< p > The < code > timer</ code > module provides timing functionality for delays, intervals, and immediate execution.</ p >
2026-01-24 22:40:22 +00:00
2026-01-25 12:09:28 +00:00
< pre >< code > import "timer" for Timer, TimerHandle</ code ></ pre >
2026-01-24 22:40:22 +00:00
< h2 > Timer Class</ h2 >
< div class = "class-header" >
< h3 > Timer</ h3 >
< p > Timer and delay functionality</ p >
</ div >
< h3 > Static Methods</ h3 >
< div class = "method-signature" >
< span class = "method-name" > Timer.sleep</ span > (< span class = "param" > milliseconds</ span > )
</ div >
< p > Pauses execution for the specified duration. The fiber suspends and other operations can proceed.</ p >
< ul class = "param-list" >
< li >< span class = "param-name" > milliseconds</ span > < span class = "param-type" > (Num)</ span > - Duration to sleep in milliseconds</ li >
</ ul >
< pre >< code > System.print("Starting...")
Timer.sleep(1000) // Wait 1 second
System.print("Done!")</ code ></ pre >
2026-01-25 12:09:28 +00:00
< div class = "method-signature" >
< span class = "method-name" > Timer.interval</ span > (< span class = "param" > milliseconds</ span > , < span class = "param" > fn</ span > ) → < span class = "type" > TimerHandle</ span >
</ div >
< p > Creates a repeating timer that calls the callback function at the specified interval. Returns a TimerHandle that can be used to stop the interval.</ p >
< ul class = "param-list" >
< li >< span class = "param-name" > milliseconds</ span > < span class = "param-type" > (Num)</ span > - Interval between calls in milliseconds</ li >
< li >< span class = "param-name" > fn</ span > < span class = "param-type" > (Fn)</ span > - Callback function to execute (no arguments)</ li >
< li >< span class = "returns" > Returns:</ span > TimerHandle for controlling the interval</ li >
</ ul >
< pre >< code > var count = 0
var handle = Timer.interval(1000) {
count = count + 1
System.print("Tick %(count)")
if (count >= 5) {
handle.stop()
}
}</ code ></ pre >
< div class = "method-signature" >
< span class = "method-name" > Timer.immediate</ span > (< span class = "param" > fn</ span > )
</ div >
< p > Schedules a function to run on the next iteration of the event loop. Useful for deferring execution without blocking.</ p >
< ul class = "param-list" >
< li >< span class = "param-name" > fn</ span > < span class = "param-type" > (Fn)</ span > - Callback function to execute (no arguments)</ li >
</ ul >
< pre >< code > System.print("Before")
Timer.immediate {
System.print("Deferred execution")
}
System.print("After")
// Output: Before, After, Deferred execution</ code ></ pre >
< h2 > TimerHandle Class</ h2 >
< div class = "class-header" >
< h3 > TimerHandle</ h3 >
< p > Handle for controlling interval timers</ p >
</ div >
< h3 > Methods</ h3 >
< div class = "method-signature" >
< span class = "method-name" > stop</ span > ()
</ div >
< p > Stops the interval timer. After calling stop, the callback will no longer be invoked.</ p >
< pre >< code > handle.stop()</ code ></ pre >
< h3 > Properties</ h3 >
< div class = "method-signature" >
< span class = "method-name" > isActive</ span > → < span class = "type" > Bool</ span >
</ div >
< p > Returns true if the interval timer is still active.</ p >
< pre >< code > if (handle.isActive) {
System.print("Timer is running")
}</ code ></ pre >
2026-01-24 22:40:22 +00:00
< h2 > Examples</ h2 >
< h3 > Basic Delay</ h3 >
< pre >< code > import "timer" for Timer
System.print("Starting...")
Timer.sleep(2000)
System.print("2 seconds later...")</ code ></ pre >
< h3 > Polling Loop</ h3 >
< pre >< code > import "timer" for Timer
import "http" for Http
var checkStatus = Fn.new {
var response = Http.get("https://api.example.com/status")
return response.json["ready"]
}
while (!checkStatus.call()) {
System.print("Waiting...")
Timer.sleep(5000) // Check every 5 seconds
}
System.print("Ready!")</ code ></ pre >
< h3 > Rate Limiting</ h3 >
< pre >< code > import "timer" for Timer
import "http" for Http
var urls = [
"https://api.example.com/1",
"https://api.example.com/2",
"https://api.example.com/3"
]
for (url in urls) {
var response = Http.get(url)
System.print("%(url): %(response.statusCode)")
Timer.sleep(1000) // 1 second between requests
}</ code ></ pre >
< h3 > Timeout Pattern</ h3 >
< pre >< code > import "timer" for Timer
import "datetime" for DateTime, Duration
var start = DateTime.now()
var timeout = Duration.fromSeconds(30)
while (true) {
var elapsed = DateTime.now() - start
if (elapsed.seconds >= timeout.seconds) {
System.print("Timeout!")
break
}
// Do work...
Timer.sleep(100)
}</ code ></ pre >
< h3 > Animation/Progress</ h3 >
< pre >< code > import "timer" for Timer
var frames = ["-", "\\", "|", "/"]
var i = 0
for (step in 1..20) {
System.write("\rProcessing %(frames[i]) ")
i = (i + 1) % 4
Timer.sleep(100)
}
System.print("\rDone! ")</ code ></ pre >
2026-01-25 12:09:28 +00:00
< h3 > Interval Timer</ h3 >
< pre >< code > import "timer" for Timer, TimerHandle
var seconds = 0
var handle = Timer.interval(1000) {
seconds = seconds + 1
System.print("Elapsed: %(seconds) seconds")
}
Timer.sleep(5000)
handle.stop()
System.print("Timer stopped")</ code ></ pre >
< h3 > Periodic Status Updates</ h3 >
< pre >< code > import "timer" for Timer, TimerHandle
import "sysinfo" for SysInfo
var handle = Timer.interval(2000) {
var freeMB = (SysInfo.freeMemory / 1024 / 1024).floor
var load = SysInfo.loadAverage[0]
System.print("Memory: %(freeMB) MB free | Load: %(load)")
}
System.print("Monitoring system (runs for 10 seconds)...")
Timer.sleep(10000)
handle.stop()</ code ></ pre >
< h3 > Immediate Execution</ h3 >
< pre >< code > import "timer" for Timer
System.print("1. Synchronous")
Timer.immediate {
System.print("3. Immediate callback")
}
System.print("2. Still synchronous")
Timer.sleep(100)</ code ></ pre >
< h3 > Self-Stopping Interval</ h3 >
< pre >< code > import "timer" for Timer, TimerHandle
var count = 0
var handle = Timer.interval(500) {
count = count + 1
System.print("Count: %(count)")
if (count >= 10) {
System.print("Stopping at %(count)")
handle.stop()
}
}</ code ></ pre >
2026-01-24 22:40:22 +00:00
< div class = "admonition note" >
< div class = "admonition-title" > Note</ div >
< p > Timer.sleep is non-blocking at the event loop level. The current fiber suspends, but other scheduled operations can continue.</ p >
</ div >
2026-01-25 12:09:28 +00:00
< div class = "admonition tip" >
< div class = "admonition-title" > Tip</ div >
< p > Use < code > Timer.interval</ code > for recurring tasks like heartbeats, polling, or animations. The returned handle allows you to stop the interval from within the callback itself.</ p >
</ div >
2026-01-24 22:40:22 +00:00
</ article >
< footer class = "page-footer" >
2026-01-25 12:09:28 +00:00
< a href = "tempfile.html" class = "prev" > tempfile</ a >
< a href = "tls.html" class = "next" > tls</ a >
2026-01-24 22:40:22 +00:00
</ footer >
</ main >
</ div >
< script src = "../js/main.js" ></ script >
</ body >
</ html >