Ranking
Original Post
[Rel] Timing module
Here's a little module for timing.

Instructions
Donwload the script and put it into data/scripts/modules



Usage
Load the module:
require("Timer")
The module provides the functions doIn, doAt and doRepeatedly to start a timer and stop to stop it.

doAt: function, time -> id
Calls function at time

Ex:
Timer.doAt(function() echo(os.clock()) end, os.time() + 1)	-- will echo(os.clock()) in one second.
doIn: function, dt -> id
Calls function in dt seconds

Ex:
Timer.doIn(function() echo(os.clock()) end, 1) 	-- will echo(os.clock()) in one second.
doRepeatedly: function, dt [, callImediately [, maxRepeats] ] -> id
Calls function every dt seconds.
If callImediately is given and true, function will be called the first time right away once.
Otherwise the first time it will be called after dt seconds.
If maxRepeats is given, function will be called at most maxRepeats times.

Ex:
Timer.doRepeatedly(function() echo(os.clock()) end, 1)		 -- will echo(os.clock()) every second until you stop it.
	
Timer.doRepeatedly(function() echo(os.clock()) end, 1, true)	 -- will echo(os.clock()) right away and then every second until you stop it.
	
Timer.doRepeatedly(function() echo(os.clock()) end, 1, false, 5) -- will echo(os.clock()) every second five times, if you don't stop it earlier.

The time or dt arguments are in seconds but accept floating point values. So if you want to do something in 500ms, do it like this:
Timer.doIn(function() ... end, 0.5)
The timer resolution is framerate dependant. At 60 fps it should be accurate to about 17ms. In general, it's accurate to 1000/fps ms.


All the do* functions return an id that can be used to stop the timer:

local id = Timer.doRepeatedly(doRepeatedly(function() echo(os.clock()) end, 1))
-- [ ... ]
Timer.stop(id)
Attached Files
Timer.lua (2.6 KB, 20 views)
Signature temporarily out of order.