Skip to content
Andrew Kvapil edited this page Nov 17, 2015 · 3 revisions

Timer Library

The timer library adds in methods for queuing timers and getting the time between frames.

Functions

new()

number CCID = timer.new( number time )

Returns the ComputerCraft timer ID of a timer that will fire in time seconds, while avoiding the duplication of timers (so preferred to os.startTimer()).

queue()

number ID = timer.queue( number time, function response )

Queues a function response to be called in time seconds, returning the ID of the timer so it can be cancelled.

cancel()

timer.cancel( number ID )

Cancels a timer started with queue().

update()

timer.update( number CCID )

Updates all running timers, triggering callbacks if necessary. Takes a ComputerCraft timer ID.

Note, this is done internally by the Application class.

Examples

Here is a simple example of an alarm clock. The application will wait for 5 seconds, then create a button with the text "Time's up!" on it and exit.

-- @define SHEETS_CORE_ELEMENTS
-- @define SHEETS_MINIFY false
-- @include sheets

-- Create our new Application
local application = Application()
-- For easier access, copy application.screen to the local 'screen'
local screen = application.screen

-- Create our alarm clock. The timer ID will actually never be used, but just for fun, store it inside alarmClockTimer
local alarmClockTimer = timer.queue( 5, function()
	-- This function will run in 5 seconds
	
	-- Create a new button on our application.screen
	local button = screen + Button( 1, 1, 20, 3, "Time's up!" )
	-- Finally, stop the application
	application:stop()
end)

-- This method will be called directly after the timer.queue() function.
-- Our queued function will run 5 seconds afterwards.
application:run()

Clone this wiki locally