Timer: Difference between revisions

From Vendetta Lua
Jump to navigationJump to search
New page: Timer class. Similar to iup.Timer == Example == timer1 = Timer() -- instantiate timer1<br> timer1:SetTimeout(10000, function() print("test") end) -- tell timer1 to print "test" after 10...
 
Wrybane (talk | contribs)
No edit summary
 
(3 intermediate revisions by 3 users not shown)
Line 4: Line 4:


== Example ==
== Example ==
 
<source lang="lua">
timer1 = Timer() -- instantiate timer1<br>
timer1 = Timer() -- instantiate timer1
timer1:SetTimeout(10000, function() print("test") end) -- tell timer1 to print "test" after 10 seconds<br>
timer1:SetTimeout(10000, function() print("test") end) -- tell timer1 to print "test" after 10 seconds
timer2 = Timer() -- instantiate timer2<br>
timer2 = Timer() -- instantiate timer2
timer2:SetTimeout(1000, function() if timer1:IsActive() then timer1:Kill() timer2:SetTimeout(2000) end end) -- tell timer2 to kill timer1 and reset itself to 2 seconds if timer1 is active.<br>
timer2:SetTimeout(1000, function() if timer1:IsActive() then timer1:Kill() timer2:SetTimeout(2000) end end) -- tell timer2 to kill timer1 and reset itself to 2 seconds if timer1 is active.
 
</source>




Line 53: Line 53:
<br><br>
<br><br>
'''Arguments:'''<br>
'''Arguments:'''<br>
'''time''' timeout of timer in msecs<br>
'''time''' timeout of timer in msecs, maximum seems to be ~596hours ( (2^31) - 1 ms, maximum of a signed! 32 bit integer ) <br>
'''callback''' function to call when time is up. if nil the previously with SetTimeout assigned function will be called and the timeout reset to the with '''time''' supplied value
'''callback''' function to call when time is up. if nil the previously with SetTimeout assigned function will be called and the timeout reset to the with '''time''' supplied value
<br><br>
<br><br>
Line 59: Line 59:
<br><br>
<br><br>
'''Example:'''<br>
'''Example:'''<br>
timer:SetTimeout(2000, function() print("test") end)<br>
<source lang="lua">timer:SetTimeout(2000, function() print("test") end)</source><br>
tell the timer "timer" to print the text "test" after two seconds
tell the timer "timer" to print the text "test" after two seconds
<br><br>
<br><br>
[[Category:Tables]]

Latest revision as of 16:28, 15 August 2011

Timer class.

Similar to iup.Timer

Example

<source lang="lua"> timer1 = Timer() -- instantiate timer1 timer1:SetTimeout(10000, function() print("test") end) -- tell timer1 to print "test" after 10 seconds timer2 = Timer() -- instantiate timer2 timer2:SetTimeout(1000, function() if timer1:IsActive() then timer1:Kill() timer2:SetTimeout(2000) end end) -- tell timer2 to kill timer1 and reset itself to 2 seconds if timer1 is active. </source>


Functions

IsActive

Definition: IsActive() -> boolean isactive

Description:
method to tell if the timer is currently running

Arguments:

Returns:
isactive true if the timer is running otherwise false

Example:


Kill

Definition: Kill() -> nil

Description:
method to stop a running timer

Arguments:

Returns:

Example:


SetTimeout

Definition: SetTimeout(int time, func callback) -> nil

Description:
method to set the timeout and function to call when the time is up

Arguments:
time timeout of timer in msecs, maximum seems to be ~596hours ( (2^31) - 1 ms, maximum of a signed! 32 bit integer )
callback function to call when time is up. if nil the previously with SetTimeout assigned function will be called and the timeout reset to the with time supplied value

Returns:

Example:
<source lang="lua">timer:SetTimeout(2000, function() print("test") end)</source>
tell the timer "timer" to print the text "test" after two seconds