sleep

From ComputerCraft
Jump to: navigation, search

sleep
Function
Syntax
sleep()
Returns nil
API BIOS globals
Source CC:Tweaked (source)

Pauses execution of the program for time seconds. As it waits for a fixed amount of ticks, time will automatically be rounded up to the nearest multiple of 0.05 seconds. If you are using coroutines or the parallel API, it will only pause execution of the current thread, not the whole program.

Note Note: Because sleep internally uses timers, it is a function that yields. This means that you can use it to prevent "Too long without yielding" errors, however, as the minimum sleep time is 0.05 seconds, it will slow your program down.

Warning Warning: The sleep function internally queues and waits for a timer event, however it does not listen for any other events. This means that any event that occurs while sleeping will be entirely discarded. If you need to recieve events while sleeping, consider using timers.

ExampleExampleSleep for a few seconds
Prints "Hello", waits 2 seconds, and then prints "world!".
Code
print("Hello")
sleep(2)
print("world!")
Output
Hello
world!
ExampleExampleSleep for the minimum amount of time
Writes 100 random numbers, one every tick.
Code
for i = 1, 100 do -- Run this 100 times
	local x, y = term.getCursorPos() 
	term.setCursorPos(1, y) -- Go back to the start of the line
	term.clearLine()
	write(math.random())
	sleep(0.05)
end