Q(uick)BASIC Statement: ON TIMER

Quick View

ON TIMER(n)

An event trapping statement that specifies a subroutine to branch to when n seconds have elapsed

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • TIMER ON
  • TIMER OFF
  • TIMER STOP
  • ON TIMER(n%) GOSUB line
Description/Parameter(s)
TIMER ON Enables timer event trapping.
TIMER OFF Disables timer event trapping.
TIMER STOP Suspends timer event trapping. Events are processed once event trapping is enabled by TIMER ON.
n% The number of seconds that elapse before ON TIMER branches to the event-trapping subroutine; a value in the range 1 through 86,400 (24 hours).
line The label or number of the first line of the event-trapping subroutine.
Example
ON TIMER(1) GOSUB TimeUpdate TIMER ON CLS PRINT "Time: "; TIME$ StartTime = TIMER WHILE TimePast < 10 TimePast = TIMER - StartTime WEND END TimeUpdate: LOCATE 1, 7: PRINT TIME$ RETURN
Syntax
  • ON TIMER(n) GOSUB {linelabel | linenumber}
Description/Parameter(s)
  • n, an integer expression with a value between 1 and 86,400, is the number of seconds in the time interval
  • linelabel or linenumber is the first line of the event-handling subroutine to branch to when the time elapses
Example

Here is a program that draws a polygon every three seconds with a random shape (three to seven sides), size, and location:

SCREEN 1 DEFINT A-Z DIM X(6), Y(6) TIMER ON 'Enable timer event trapping. ON TIMER(3) GOSUB Drawpoly 'Draw a new polygon every 'three seconds. PRINT "Press any key to end program" INPUT "Press <RETURN> to start",Test$ DO LOOP WHILE INKEY$ = "" 'End program if any key pressed. END Drawpoly: CLS 'Erase old polygon. N = INT(5 * RND + 2) 'N is random number from 2 to 6. FOR I = 0 TO N X(I) = INT(RND * 319) 'Get coordinates of vertices of Y(I) = INT(RND * 199) 'polygon. NEXT PSET (X(N), Y(N)) FOR I = 0 TO N LINE -(X(I), Y(I)),2 'Draw new polygon. NEXT RETURN

See also:

Syntax
  • ON TIMER(n&) GOSUB {linenumber | linelabel}
Description/Parameter(s)
n& A numeric expression with a value between 1 and 86,400 (24 hours). The number of seconds in the time interval.
linenumber or linelabel The first line of the event-handling routine to branch to when n& seconds have passed.

Usage Notes

  • The maximum number of seconds that can be specified for the time interval, n&, is the number of seconds in a 24-hour day (86,400).
  • The ON TIMER statement specifies only the start of an event-trapping routine. The TIMER Statements determine whether the routine is called and how events are handled when trapping is off:
    TIMER ONEnables event trapping. Event trapping occurs only after a TIMER ON statement is executed.
    TIMER OFFDisables event trapping. Even if an event takes place, it is not remembered.
    TIMER STOPSuspends event trapping. Any events that occur are remembered and are trapped as soon as a TIMER ON statement is executed.
  • If your program contains event-handling statements and you are compiling from the BC command line, use the BC /W or /V option. (The /W option checks for events at every label or line number; the /V option checks at every statement.) If you do not use these options and your program contains event traps, BASIC generates the error message, "ON event without /V or /W on command line."
  • The RETURN linenumber or RETURN linelabel forms of RETURN can be used to return to a specific line from the trapping routine. Use this type of return with care, however, because any other GOSUB, WHILE, or FOR statements active at the time of the trap remain active. BASIC may generate error messages such as "NEXT without FOR." In addition, if an event occurs in a procedure, a RETURN linenumber or RETURN linelabel statement cannot get back into the procedure because the line number or label must be in the module-level code.
Example

This example uses the TIMER and ON TIMER statements to enable timer event trapping. The program draws a polygon with a random shape, size, and location every three seconds.

SCREEN 1 DEFINT A-Z DIM X(6), Y(6) TIMER ON 'Enable timer event trapping. ON TIMER(3) GOSUB Drawpoly 'Draw a new polygon every 'three seconds. PRINT "Press any key to end program" INPUT "Press <RETURN> to start", Test$ DO LOOP WHILE INKEY$ = "" 'End program if any key pressed. END Drawpoly: CLS 'Erase old polygon. N = INT(5 * RND + 2) 'N is random number from 2 to 6. FOR I = 0 TO N X(I) = INT(RND * 319) 'Get coordinates of vertices of Y(I) = INT(RND * 199) 'polygon. NEXT PSET (X(N), Y(N)) FOR I = 0 TO N LINE -(X(I), Y(I)), 2 'Draw new polygon. NEXT RETURN