2026-01-26 05:12:14 +01:00
{# retoor < retoor @ molodetz . nl > #}
{% extends 'page.html' %}
2026-01-24 23:40:22 +01:00
2026-01-26 05:12:14 +01:00
{% set page_title = "timer" %}
{% set breadcrumb = [{"url": "api/index.html", "title": "API Reference"}, {"title": "timer"}] %}
{% set prev_page = {"url": "api/tempfile.html", "title": "tempfile"} %}
{% set next_page = {"url": "api/tls.html", "title": "tls"} %}
{% block article %}
2026-01-24 23:40:22 +01:00
< h1 > timer</ h1 >
2026-01-25 13:09:28 +01:00
< p > The < code > timer</ code > module provides timing functionality for delays, intervals, and immediate execution.</ p >
2026-01-24 23:40:22 +01:00
2026-01-25 13:09:28 +01:00
< pre >< code > import "timer" for Timer, TimerHandle</ code ></ pre >
2026-01-24 23:40:22 +01: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 13:09:28 +01: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 23:40:22 +01: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 13:09:28 +01: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 23:40:22 +01: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 13:09:28 +01: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-26 05:12:14 +01:00
{% endblock %}