BB___TIMER

=TIMER= toc

Description
> The timer causes a branch label or sub name to be executed at a set interval. > The interval should be integer and is measured in milliseconds (ms). 1000 ms is one second.

Syntax
Setting next timer event: This one sets timer off:

Hints

 * //**There is only one timer.**// (!!!) Attempting to set another event at another time will reset it, see below (//Resetting the Timer.//)
 * //**Turning off the Timer.**// The timer will continue to "fire" until it is turned off with a **timer 0** statement.
 * //**Turning off the Timer-2.**// If timer set to 500 and at time 300 turned off, time event at 500 will not occur.
 * //**"Stacking" Timer events.**// If you have repeated timer events but you program is busy, (ex drawing routine takes too long), timer events will "stack". They will fire at once after "busy" part. Turning off timer does not prevent "stacked" events from firing (see example).
 * //**Resetting the Timer.**// If timer set to 500 and at time 300 issued "timer 400, [branchLabel]", time event at 500 will not occur, but event at around 700 (300+400) will fire.


 * //**Repeating Tasks.**// The timer can be used for animation or for other repetitive tasks. The timer will continue to "fire" until it is turned off with a timer 0 statement.
 * //**Timer precision.**// Timer event is provided by Windows; if Windows is busy, timer event could be late.
 * //**Timer precision-2.**// Windows timer precision often worse then 1 ms. For Win 9x, typical precison is 56 ms (roughly 18 times per second); for Win 2000, 10 ms; for Win XP, 16ms (around 60 ticks per second). My guess that typical for Vista, Win 7 still 16 ms. That means that next timer event will be not before next 16 ms passed, even if you set interval to lesser time. "timer 0, [branchLabel]" makes minimal wait possible - that is, often 16 ms. (see example).
 * //**Bug with Timer Sub event handler.**// There is a bug with using Timer with sub event (with GUI?) handler that make program hang if GUI window is moved or resized: LB Bugs::Timer - Sub as Event Handler

Example
A simple timer example using a branch label handler. code format="lb" 'set a timer to fire in 3 seconds 'using branch label event handler timer 3000, [itHappened] 'wait here wait

[itHappened] 'deactivate the timer timer 0 confirm "It happened! Do it again?"; answer if answer then 'reactivate the timer timer 3000, [itHappened] wait end if   end code

An example using a subroutine handler. code format="lb" 'set a timer to fire in 3 seconds

'using subroutine event handler timer 3000, itHappened 'wait here wait

sub itHappened 'deactivate the timer timer 0 confirm "It happened! Do it again?"; answer if answer then 'reactivate the timer timer 3000, itHappened end if   end sub code

An example for stacked timer events / using timer inside a loop (if you uncomment SCAN) code format="lb" global t0 t0=time$("ms") timer 200, clickTimer 'busy loop prevents timer from firing while time$("ms")-t0 < 1000 'Uncomment scan so timer will fire inside a loop 'scan i=i+1 if i mod 50 =0 then print "."; if i mod (50*50) =0 then print wend print print "Time passed ";time$("ms")-t0 timer 0 'don't forget to turn timer off 'now, all stacked timer event will fire wait

sub clickTimer print "Timer!"; time$("ms")-t0 end sub code

Example showing minimal possible time interval: code format="lb" i = 0 timer 0, [nextTick] t0=time$("ms") wait

[nextTick] dt = time$("ms")-t0 t0=time$("ms") print dt;" "; i = i+1 if i >=10 then timer 0: end wait code (outputs something like 16 16 15 16 15 16 16 15 16 16 )

Useful Functions
code format="lb" Place a useful function using this command here code