timedwait

timedwait(testcb::Function, secs::Float64; pollint::Float64=0.1)

Waits till testcb returns true or for secs seconds, whichever is earlier. testcb is polled every pollint seconds.

Examples

In the Julia programming language, the function timedwait(testcb::Function, secs::Float64; pollint::Float64=0.1) is used to wait until the testcb function returns true or for a specified number of seconds, whichever is earlier. The testcb function is polled every pollint seconds.

julia> function test_condition()
           return false
       end

julia> timedwait(test_condition, 5.0; pollint=0.5)
false

In this example, the test_condition function always returns false. The timedwait function waits for a maximum of 5.0 seconds and polls the test_condition function every 0.5 seconds. Since test_condition never returns true, the timedwait function returns false after the specified timeout.

julia> function test_condition()
           return true
       end

julia> timedwait(test_condition, 10.0; pollint=2.0)
true

In this example, the test_condition function always returns true. The timedwait function waits for a maximum of 10.0 seconds and polls the test_condition function every 2.0 seconds. Since test_condition returns true immediately, the timedwait function returns true without waiting for the full timeout duration.

Common mistake example:

julia> function test_condition()
           return "true"
       end

julia> timedwait(test_condition, 3.0; pollint=0.2)
ERROR: MethodError: no method matching convert(::Type{Bool}, ::String)

In this example, the test_condition function returns a string "true" instead of a boolean value. The timedwait function expects the testcb function to return a boolean value. Make sure the testcb function returns either true or false to avoid such errors.

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.

*Required Field
Details

Checking you are not a robot: