Skip to content

Timer timer

Creates and manages recurring timers.

;; Create a timer that runs every 1000ms (1 second)
 (timer 1000 0 (fn [t] (println "Timer tick:" t)))
0
;; Create a timer that runs 5 times (returns next timer ID)
(timer 1000 5 (fn [t] (println "Count:" t)))
1

;; Remove a timer by ID
(timer 0)
null

;; Create infinite timer (runs until removed)
(timer 5000 0 (fn [t] (do (println "Background task at:" t) (gc))))
2

Syntax

;; Create timer
(timer interval times callback)
;; Remove timer
(timer id)
- interval: Time between executions in milliseconds - times: Number of executions (0 for infinite) - callback: Lambda to execute (receives current time as argument) - id: Timer ID to remove

Warning

  • Minimum interval is system-dependent
  • Callback execution time affects next interval
  • Timer continues until removed or count reached
  • Timers are cleared when process exits