### Create and manage one-time timed events with cron.lua Source: https://github.com/kikito/cron.lua/blob/master/README.md This code illustrates creating a one-time event using `cron.after`. It shows how to initialize a clock that triggers a callback after a specified time, pass arguments to the callback, update the clock's internal timer, and reset it. The example also demonstrates checking if the event has expired. ```lua local cron = require 'cron' local function printMessage() print('Hello') end -- the following calls are equivalent: local c1 = cron.after(5, printMessage) local c2 = cron.after(5, print, 'Hello') c1:update(2) -- will print nothing, the action is not done yet c1:update(5) -- will print 'Hello' once c1:reset() -- reset the counter to 0 -- prints 'hey' 5 times and then prints 'hello' while not c1:update(1) do print('hey') end ``` -------------------------------- ### Create and manage periodic timed events with cron.lua Source: https://github.com/kikito/cron.lua/blob/master/README.md This example demonstrates creating a periodic event using `cron.every`. It shows how to set up a clock that repeatedly executes a callback at a given interval. The snippet also illustrates how updating the clock with a larger delta can trigger the callback multiple times if the accumulated time exceeds the interval. ```lua local cron = require 'cron' local function printMessage() print('Hello') end -- Create a periodical clock: local c3 = cron.every(10, printMessage) c3:update(5) -- nothing (total time: 5) c3:update(4) -- nothing (total time: 9) c3:update(12) -- prints 'Hello' twice (total time is now 21) ``` -------------------------------- ### Lua: Create and manage periodic execution clocks Source: https://context7.com/kikito/cron.lua/llms.txt Illustrates creating periodic clocks with `cron.every` for repeated callback execution at fixed intervals. Shows how multiple callbacks can trigger within a single update if sufficient delta time is provided. Includes examples of resetting the internal timer to a specific value. ```lua local cron = require 'cron' -- Basic periodic execution local tick_count = 0 local ticker = cron.every(1, function() tick_count = tick_count + 1 print('Tick #' .. tick_count) end) -- Periodic execution with parameters local damage_clock = cron.every(2, function(damage, target) print(target .. ' takes ' .. damage .. ' damage') end, 10, 'Enemy') -- Update over time ticker:update(0.5) -- Prints nothing (0.5 < 1) ticker:update(0.7) -- Prints "Tick #1" (total: 1.2, triggers once) ticker:update(2.5) -- Prints "Tick #2" and "Tick #3" (total: 3.7, triggers twice) damage_clock:update(5) -- Prints damage message twice (5 / 2 = 2 triggers) -- Reset internal timer to specific value ticker:reset(0.9) -- Next update of 0.1+ will trigger ticker:update(0.2) -- Triggers immediately since 0.9 + 0.2 >= 1.0 ``` -------------------------------- ### Run cron.lua specs with busted Source: https://github.com/kikito/cron.lua/blob/master/README.md This command demonstrates how to execute the project's specifications using the 'busted' testing framework. It requires navigating to the project's directory where the 'spec' folder is located and then running the 'busted' command. ```bash cd path/where/the/spec/folder/is busted ``` -------------------------------- ### Require cron.lua library Source: https://github.com/kikito/cron.lua/blob/master/README.md This snippet demonstrates how to include the cron.lua library into your project. It assumes the 'cron.lua' file is accessible and assigns the returned module to a local variable named 'cron'. ```lua local cron = require 'cron' ``` -------------------------------- ### Lua: Create and manage one-time delayed execution clocks Source: https://context7.com/kikito/cron.lua/llms.txt Demonstrates creating a one-time clock using `cron.after`, passing arguments to the callback, and manually updating the clock in a loop. It also shows how to reset and reuse a clock. This is typically done within a game or animation loop. ```lua local cron = require 'cron' -- Basic delayed execution local clock = cron.after(5, function() print('5 time units have passed!') end) -- Pass parameters to callback local greet_clock = cron.after(3, function(name, age) print('Hello ' .. name .. ', age ' .. age) end, 'Alice', 25) -- Update loop (typically in game/animation loop) local total_time = 0 while total_time < 10 do local dt = 0.016 -- ~60fps frame time local expired = clock:update(dt) greet_clock:update(dt) if expired then print('Clock has expired and callback was executed') end total_time = total_time + dt end -- Reset and reuse clock clock:reset() clock:update(5) -- Will execute callback again ``` -------------------------------- ### Lua: Update clock timers and check expiration status Source: https://context7.com/kikito/cron.lua/llms.txt Shows how to use the `clock:update(dt)` method to advance timers for both one-time (`after`) and periodic (`every`) clocks. Demonstrates checking the boolean return value for one-time clocks to determine expiration and how periodic clocks handle accumulated time for multiple executions. ```lua local cron = require 'cron' -- One-time clock expiration checking local expired = false local after_clock = cron.after(10, function() print('Timer completed!') end) while not expired do expired = after_clock:update(1) print('Still waiting... expired=' .. tostring(expired)) end -- Output: Several "Still waiting... expired=false" then "Timer completed!" and "Still waiting... expired=true" -- Periodic clock with variable delta times local execution_count = 0 local every_clock = cron.every(2, function() execution_count = execution_count + 1 end) every_clock:update(1.5) -- execution_count = 0 (not enough time) every_clock:update(0.8) -- execution_count = 1 (total 2.3, triggers once) every_clock:update(5.0) -- execution_count = 3 (triggers twice more) print('Total executions: ' .. execution_count) -- Output: Total executions: 3 ``` -------------------------------- ### Resetting Lua Cron Clocks Source: https://context7.com/kikito/cron.lua/llms.txt Demonstrates how to reset Lua cron clocks using the `reset()` method. This function allows reinitializing a clock's internal timer to zero or a specific value, useful for restarting events or synchronizing multiple clocks. The callback function is not invoked during a reset. ```lua local cron = require 'cron' -- Reset after clock to reuse it local notification = cron.after(5, function() print('Notification displayed') end) notification:update(5) -- Executes callback print(notification:update(1)) -- Output: true (already expired) notification:reset() -- Reset to 0 print(notification:update(1)) -- Output: false (not expired yet) notification:update(4) -- Executes callback again -- Reset periodic clock with specific starting value local heartbeat = cron.every(3, function() print('heartbeat') end) heartbeat:update(2) -- Internal timer now at 2 heartbeat:reset(2.9) -- Set timer to 2.9 (close to trigger) heartbeat:update(0.2) -- Triggers immediately (2.9 + 0.2 = 3.1) -- Synchronize multiple clocks local clock1 = cron.every(5, function() print('Clock 1') end) local clock2 = cron.every(5, function() print('Clock 2') end) clock1:update(2) clock2:update(3) -- Now they're out of sync, synchronize them: clock1:reset(0) clock2:reset(0) -- Both clocks now start from the same point ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.