Updated April 11, 2023
Introduction to Lua sleep
Whenever there is a need to pause the program that we are executing for a certain number of seconds without making use of busy or waiting, then we make use of a function called sleep() function in Lua, and this sleep() function is not a built-in function in Lua but there are several ways to implement this sleep() function depending on the operating system or platform on which the Lua program is running and any program intending to call sleep() function must implement the sleep() function in the program before calling it and sleep() function can be implemented on Windows, Linux, and Mac operating system.
The syntax to define sleep() function in Lua on a Windows operating system is as follows:
local clock = os.clock
function sleep(n)
local t0 = clock()
while clock() - t0 <= n do end
end
sleep(number_of_seconds)
where os. clock returns the number of seconds for the CPU time for the program and
n is the parameter that takes the number of seconds we want our program to be paused.
How to Working of sleep() function in Lua?
Working of sleep() function in Lua is as follows:
- Whenever there is a need to pause the program that we are executing for a certain number of seconds without making use of busy or waiting, then we make use of a function called sleep() function in Lua.
- The sleep() function is not a built-in function in Lua.
- There are several ways to implement the sleep() function depending on the operating system or platform on which the Lua program is running.
- Any program intending to call sleep() function must implement the sleep() function in the program before calling it in the program.
- The sleep() function can be implemented on Windows, Linux, and Mac operating systems.
Examples
Below are some different examples:
Example #1
Lua program to demonstrate the working of sleep() function by implementing it on a Windows operating system and pausing the program for 30 seconds before it prints the second statement in the program:
Code:
--implementing the sleep() function on an windows operating system
local clock = os.clock
function sleep(n)-- seconds
local t0 = clock()
while clock() - t0 <= n do end
end
--printing the first statement and then calling sleep() function to pause the program for 30 seconds before it can print the second statement
print("Welcome to\n")
print("\nPlease wait for 30 seconds\n")
sleep(30)
print("\nEDUCBA\n")
The output of the above program is as shown in the snapshot below:
In the above program, we are implementing the sleep() function on a Windows operating system. Then we are printing a statement and then calling sleep() function to pause the program for 30 seconds before it can print the second statement as the output on the screen. The output is shown in the snapshot above.
Example #2
Lua program to demonstrate sleep() function in which we create an array and iterate through the elements of the array and display each element of the array as the output on the screen after pausing the program for 20 seconds:
Code:
--implementing the sleep() function on an windows operating system
local clock = os.clock
function sleep(n)-- seconds
local t0 = clock()
while clock() - t0 <= n do end
end
--printing the elements in the list one after the other by pausing the program for 20 seconds after printing each element
mylist = {"Welcome", "to", "EDUCBA"}
for a = 1,3 do
print("\n")
print(mylist[a])
print("\nPlease wait for 20 seconds\n")
sleep(20)
end
The output of the above program is as shown in the snapshot below:
In the above program, we are implementing the sleep() function on a Windows operating system. Then we are creating an array and then iterating through the array to display the elements of the array one after the other by pausing the program for 20 seconds after displaying each element as the output on the screen. The output is shown in the snapshot above.
Example #3
Lua program to demonstrate sleep() function in which we create an array and iterate through the elements of the array and display each element of the array as the output on the screen after pausing the program for 10 seconds:
Code:
--implementing the sleep() function on an windows operating system
local clock = os.clock
function sleep(n)-- seconds
local t0 = clock()
while clock() - t0 <= n do end
end
--printing the elements in the list one after the other by pausing the program for 20 seconds after printing each element
mylist = {"Learning", "is", "fun"}
for a = 1,3 do
print("\n")
print(mylist[a])
print("\nPlease wait for 10 seconds\n")
sleep(10)
end
The output of the above program is as shown in the snapshot below:
In the above program, we are implementing the sleep() function on a Windows operating system. Then we are creating an array and then iterating through the array to display the elements of the array one after the other by pausing the program for 10 seconds after displaying each element as the output on the screen. The output is shown in the snapshot above.
Example #4
Lua program to demonstrate the working of sleep() function by implementing it on a Windows operating system and pausing the program for 30 seconds before it prints the second statement in the program:
Code:
--implementing the sleep() function on an windows operating system
local clock = os.clock
function sleep(n)-- seconds
local t0 = clock()
while clock() - t0 <= n do end
end
--printing the first statement and then calling sleep() function to pause the program for 30 seconds before it cana print the second statement
print("Learning is\n")
print("\nPlease wait for 30 seconds\n")
sleep(30)
print("\nfun\n")
The output of the above program is as shown in the snapshot below:
In the above program, we are implementing the sleep() function on a Windows operating system. Then we are printing a statement and then calling sleep() function to pause the program for 30 seconds before it can print the second statement as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of sleep() function in Lua through definition, syntax, and working of sleep() function in Lua with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “Lua sleep” was beneficial to you. You can view EDUCBA’s recommended articles for more information.