Block the current task for a specified number of seconds. The minimum sleep time is 1 millisecond or input of 0.001.


  1. Sleep for a specified number of seconds:

    julia> sleep(2.5)

    This example suspends the current task for 2.5 seconds.

  2. Delay execution in a loop:

    julia> for i in 1:5
              println("Iteration $i")

    In this example, the code waits for 1 second before executing each iteration of the loop.

  3. Combine sleep with other operations:

    julia> println("Starting...")
    julia> sleep(0.5)
    julia> println("Finished!")

    The code above introduces a delay of 0.5 seconds between the "Starting..." and "Finished!" print statements.

Common mistake example:

julia> sleep(-1)
ERROR: DomainError with -1:
Seconds must be non-negative

In this example, a negative value is provided as the input to sleep. The sleep time must be a non-negative value, and a negative value will result in a DomainError. Always ensure that the sleep time is a valid non-negative value 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

Checking you are not a robot: