Timer(::Function,delay,repeat=0)
Timer(callback::Function, delay, repeat=0)
Create a timer to call the given callback function. The callback is passed one argument, the timer object itself. The callback will be invoked after the specified initial delay, and then repeating with the given repeat interval. If repeat is 0, the timer is only triggered once. Times are in seconds. A timer is stopped and has its resources freed by calling close on it.
Examples
Examples of using the Timer function in Julia:
-
Create a timer that wakes up a waiting task after a specified delay:
julia> t = Timer(2.5) Base.Timer(TimerCondition(Any[Task (runnable) @0x00007f9e508e4f50]), false, 2.5, 0.0)This example creates a timer
tthat will wake up any waiting tasks after a delay of 2.5 seconds. -
Create a repeating timer that wakes up tasks at a specified interval:
julia> t = Timer(1.0, repeat=3) Base.Timer(TimerCondition(Any[Task (runnable) @0x00007f9e508e4f50]), false, 1.0, 3)This example creates a repeating timer
tthat will wake up any waiting tasks every 1 second for a total of 3 times. -
Check if a timer is still active using
isopen:julia> t = Timer(5.0) Base.Timer(TimerCondition(Any[Task (runnable) @0x00007f9e508e4f50]), false, 5.0, 0.0) julia> isopen(t) true julia> close(t) julia> isopen(t) falseThis example demonstrates how to use the
isopenfunction to check if a timertis still active. The timer is closed using theclosefunction.
Common mistake example:
julia> t = Timer(-1.0)
ERROR: ArgumentError: Timer delay cannot be negative
In this example, the provided delay for the timer is negative, which is not allowed. Make sure to provide a non-negative delay value when creating a timer.
See Also
:@time, :@timed, :@timev, now, sleep, tic, time, timedwait, Timer, time_ns, toc, toq,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.