Delay the execution of a function by t seconds.
Documentation
Package function
delay(fn, t)(function)- Delay the execution of a function by t seconds.
- Parameters:
- Returns: Function that will execute
delay~fnafter t seconds -handle. (function)
Other items
Notes
- Based on a simple busy-wait loop, which is not efficient for long delays.
- This module is likely to timeout if used in Scribunto.
See also
--- Delay the execution of a function by t seconds.
-- @module delay
-- @param {function} fn The function to execute after the delay.
-- @param {number} t The delay in seconds before executing the function.
-- @return {function} Function that will execute @{delay~fn} after t seconds - @{handle}.
-- @release stable
-- @author [[User:8nml|8nml]]
-- @author [[User:TheReelDevs|TheReelDevs]]
-- @note Based on a simple busy-wait loop, which is not efficient for long delays.
-- @note This module is likely to timeout if used in Scribunto.
-- @see https://github.com/rxi/tick/blob/master/tick.lua
--- A simple function that waits t seconds.
-- @param {number} t The number of seconds to wait.
-- @return {nil} Nothing.
local function wait(t)
local start = os.time()
repeat until os.time() > start + t
end
--- Function handle generated by @{delay}.
-- @function handle
-- @error[line=28] 'Expected function for argument #1'
-- @error[line=31] 'Expected number for argument #2'
-- @return {nil} Nothing.
return function(fn, t)
if type(fn) ~= "function" then
error("Expected function for argument #1", 2)
end
if type(t) ~= "number" then
error("Expected number for argument #2", 2)
end
wait(t)
fn()
end