sleep

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.

sleep
Function
Syntax
sleep(
  • time : number
)

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

 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: 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 receive events while sleeping, consider using timers.

ExampleSleep for a few seconds
Prints "Hello", waits 2 seconds, and then prints "world!".
Code
<nowiki>
print("Hello")
sleep(2)
print("world!")
    </nowiki>
Output
<nowiki>Hello
world!</nowiki>
ExampleSleep for the minimum amount of time
Writes 100 random numbers, one every tick.
Code
<nowiki>
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
    </nowiki>
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.