ON TIMER(n)

From QB64 Wiki
Revision as of 19:09, 18 January 2011 by imported>Clippy
Jump to navigation Jump to search

The ON TIMER statement sets up a timed event to be repeated at specified intervals.


Qbasic Syntax: ON TIMER(seconds%) GOSUB {linelabel|linenumber}


QB64 Syntax #1: ON TIMER([number%,] seconds!) GOSUB {linelabel|linenumber}
QB64 Syntax #2: ON TIMER([number%,] seconds!) SubProcedure [parameter[,...]]


Description

QBasic and QB64 Information
  • In Qbasic the INTEGER seconds parameter can be from 1 to 86400 seconds(one day).
  • A TIMER ON statement must be made before an ON TIMER event is enabled in QB or QB64.
  • TIMER STOP remembers previous events when enabled by a TIMER ON statement and the ON TIMER statement(s) may be executed immediately if a timer event has occurred.
  • TIMER OFF disables timer event trapping. Events will not be remembered in a subsequent ON TIMER statement.
  • ON TIMER events will interrupt a SLEEP call and return to normal program procedures.
  • Qbasic can only use one TIMER event at a time and code must be in the main code.
QB64 Specific Information
  • Can use multiple timers and SINGLE floating point second values down to one millisecond(.001).
  • MUST use the _FREETIMER function to assign free timer numbers. Use specific variables(or an array) for different TIMERs.
  • If the TIMER number is omitted, the TIMER used is the base timer. That TIMER cannot be freed using TIMER[0] FREE. No number reference is used for the base timer and keeps QB64 compatible with Qbasic.
  • Direct SUB procedure calls are also allowed without using CALL.
  • Parameter values are passed by value and should be SHARED or literal values!
  • Use TIMER(n) FREE to release a timer event after it has been turned off and is no longer used.
  • QB64 allows TIMER statements to also be inside of SUB and FUNCTION procedures.
  • Does not currently interrupt SLEEP calls.
  • $CHECKING:OFF can disable QB64 event checking. Use it ONLY with errorless code that needs every CPU cycle!



Example 1: How to update the time while printing at the same time in a program.

TIMER ON ' enable timer event trapping LOCATE 4, 2 ' set the starting PRINT position ON TIMER(10) GOSUB Clock ' set procedure execution repeat time DO WHILE INKEY$ = "": PRINT "A"; : SLEEP 6: LOOP TIMER OFF SYSTEM Clock: row = CSRLIN ' Save current print cursor row. col = POS(0) ' Save current print cursor column. LOCATE 2, 37: PRINT TIME$; ' print current time at top of screen. LOCATE row, col ' return to last print cursor position RETURN

NOTE: SLEEP will be interrupted in Qbasic.


Example 2: Using a numbered TIMER to check the mouse button press status in QB64.

DIM SHARED Button AS LONG 'share variable value with Sub t1 = _FREETIMER 'get a timer number from _FREETIMER ONLY! ON TIMER(t1, .05) MouseClick TIMER(t1) ON DO LOCATE 1, 1 IF Button THEN PRINT "Mouse button"; Button; "is pressed."; ELSE PRINT SPACE$(70) END IF _DISPLAY LOOP UNTIL INKEY$ = CHR$(27) TIMER(t1) OFF TIMER(t1) FREE 'release timer END SUB MouseClick DO WHILE _MOUSEINPUT IF _MOUSEBUTTON(1) THEN COLOR 10: Button = 1 ELSEIF _MOUSEBUTTON(2) THEN COLOR 12: Button = 2 ELSE Button = 0 END IF LOOP END SUB


See also



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page