Q(uick)BASIC Statements: TIMER

Quick View

TIMER

An event trapping statement that enables, disables, or inhibits timer event trapping

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
  • TIMER ON
  • TIMER OFF
  • TIMER STOP
Description/Parameter(s)

TIMER ON enables timer event trapping by an ON TIMER statement. While trapping is enabled, a check is made after every statement to see if the specified time has elapsed. If it has, the ON TIMER event-handling routine is executed.

TIMER OFF disables timer event trapping. If an event takes place, it is not remembered if a subsequent TIMER ON is executed.

TIMER STOP disables the timer event trapping. However, if an event occurs, it is remembered and the ON TIMER event-handling routine is executed as soon as trapping is reenabled with TIMER ON.

Example

The following example displays the time of day on line 1, and updates the display once every three seconds:

'*** Programming example: TIMER statement *** 'To stop the program, press any key ' TIMER ON ON TIMER(3) GOSUB Display DO WHILE INKEY$ = "" : LOOP END Display: Oldrow = CSRLIN 'Save current row. Oldcol = POS(0) 'Save current column. LOCATE 1,1 : PRINT TIME$; LOCATE Oldrow,Oldcol 'Restore row & column. RETURN
Syntax
  • TIMER ON
  • TIMER OFF
  • TIMER STOP
Description/Parameter(s)
TIMER ON Enables timer-event trapping. statement.
TIMER OFF Disables the event trap. Even if an event takes place, it is not remembered.
TIMER STOP Suspends the event trap. Any events that occur are remembered and are trapped as soon as a TIMER ON statement is executed.

Usage Notes

  • TIMER ON enables timer-event trapping. A timer event occurs when n& seconds have elapsed (as specified in the ON TIMER statement). If a timer event occurs after a TIMER ON statement, the routine specified in the ON TIMER statement is executed.
  • TIMER OFF disables timer-event trapping. No timer-event trapping takes place until another TIMER ON statement is executed. Events occurring while trapping is off are ignored.
  • TIMER STOP suspends timer-event trapping. No trapping takes place until another TIMER ON statement is executed. Events occurring while trapping is off are remembered and processed when the next TIMER ON statement is executed. However, remembered events are lost if TIMER OFF is executed.
  • When a timer-event trap occurs (that is, the GOSUB is performed), an automatic TIMER STOP is executed so that recursive traps cannot take place. The RETURN from the trapping routine automatically performs a TIMER ON statement unless an explicit TIMER OFF was performed inside the routine.
  • 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."
  • For more information, see Chapter 9, "Event Handling" in the Programmer's Guide.
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