Difference between revisions of "TIMER (statement)"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
A '''TIMER''' statement enables, turns off or stops timer event trapping. Qbasic only uses one timer, but '''QB64''' can have many.
+
A '''TIMER''' statement enables, turns off or stops timer event trapping. Qbasic only uses the base timer, but '''QB64''' can run many.
  
  
''Syntax 1:'' TIMER ON
+
QB {{PageSyntax}}
  
''Syntax 2:'' TIMER OFF
+
:::TIMER {ON|STOP|OFF}
  
''Syntax 3:'' TIMER STOP
+
QB64 {{PageSyntax}}
  
 +
:::TIMER(''number%'') {ON|STOP|OFF|FREE}
  
  
* TIMER ON enables timer event trapping by an [[ON TIMER (n)]] statement. While trapping is enabled, a check is made after every code statement to see if the specified time has elapsed. If it has elapsed, the ON TIMER event-handling routine is executed.
+
{{Parameters}}
 +
* ''number'' denotes a specific numbered timer event in '''QB64 only'''. QB64 can run many timer events at once including the base timer.
 +
* TIMER ON enables event trapping of an [[ON TIMER (n)]] statement. While enabled, a check is made after every code statement to see if the specified time has elapsed and the ON TIMER [[GOSUB]] (or [[SUB]] in QB64) procedure is executed.
 +
* TIMER STOP disables timer event trapping. When an event occurs while stopped, it is remembered. If timer events are turned back on later, any remembered events are immediately executed.
 +
* TIMER OFF turns timer event trapping completely off and no subsequent events are remembered.
  
* TIMER OFF disables timer event trapping. If an event takes place, it is not remembered if a subsequent TIMER ON statement is attempted.
+
<center>'''QB64 only'''</center>
 +
* Get a TIMER number from [[_FREETIMER]] ONLY except when the base timer(no number or 0) is used. Use specific variables or an array to hold each event number value for later reference.
 +
* If the TIMER number is omitted or 0, the TIMER used is the base timer.
 +
* Specific TIMER events can be enabled, suspended, turned off or freed using [[TIMER (statement)|TIMER(n)]] ON, STOP, OFF or FREE.
 +
* TIMER(n) '''FREE''' clears a specific timer event when it is no longer needed. '''The base TIMER or TIMER(0) cannot be freed!'''
  
* TIMER STOP disables timer event trapping. However, if an event occurs, it is remembered and the ON TIMER event-handling routine is executed as soon as event trapping is re-enabled using the TIMER ON statement.
 
  
* '''QB64''' uses TIMER(number, duration) ON, OFF, STOP or FREE(clears the timer event)
+
<center>'''QB64 Timing Alternatives'''</center>
 +
* The [[TIMER]] function can be used to find timed intervals down to 1 millisecond(.001) accuracy.
 +
* The [[_DELAY]] statement can be used to delay program execution for intervals down to milliseconds.
 +
* [[_LIMIT]] can slow down loops to a specified number of frames per second. This can also alleviate a program's CPU usage.
  
 +
 +
''Example:'' How to update the time while [[PRINT|printing]] at the same time in a program.
 +
{{CodeStart}} '' ''
 +
  TIMER ON ' enable timer event trapping
 +
  LOCATE 4, 2 ' set the starting PRINT position
 +
  {{Cl|ON TIMER(n)|ON TIMER}}(10) GOSUB Clock ' set procedure execution repeat time
 +
  DO WHILE INKEY$ = "": PRINT "A"; : SLEEP 6: LOOP
 +
  TIMER OFF
 +
  {{Cl|SYSTEM}}
 +
 +
Clock:
 +
  row = {{Cl|CSRLIN}} ' Save current print cursor row.
 +
  col = {{Cl|POS|POS(0)}} ' Save current print cursor column.
 +
  LOCATE 2, 37: PRINT {{Cl|TIME$}}; ' print current time at top of screen.
 +
  LOCATE row, col ' return to last print cursor position
 +
{{Cl|RETURN}} '' ''
 +
{{CodeEnd}}
 +
: NOTE: SLEEP will be interrupted in Qbasic.
  
  
 
''See also:''  
 
''See also:''  
  
[[ON TIMER (n)]], [[TIMER]], [[_DELAY]]
+
* [[ON TIMER(n)]], [[TIMER]](function)
 +
 
 +
* [[_DELAY]], [[_LIMIT]]
  
  
  
 
{{PageNavigation}}
 
{{PageNavigation}}

Latest revision as of 14:09, 17 September 2016

A TIMER statement enables, turns off or stops timer event trapping. Qbasic only uses the base timer, but QB64 can run many.


QB == Syntax ==

TIMER {ON|STOP|OFF}

QB64 == Syntax ==

TIMER(number%) {ON|STOP|OFF|FREE}


Parameters

  • number denotes a specific numbered timer event in QB64 only. QB64 can run many timer events at once including the base timer.
  • TIMER ON enables event trapping of an ON TIMER (n) statement. While enabled, a check is made after every code statement to see if the specified time has elapsed and the ON TIMER GOSUB (or SUB in QB64) procedure is executed.
  • TIMER STOP disables timer event trapping. When an event occurs while stopped, it is remembered. If timer events are turned back on later, any remembered events are immediately executed.
  • TIMER OFF turns timer event trapping completely off and no subsequent events are remembered.
QB64 only
  • Get a TIMER number from _FREETIMER ONLY except when the base timer(no number or 0) is used. Use specific variables or an array to hold each event number value for later reference.
  • If the TIMER number is omitted or 0, the TIMER used is the base timer.
  • Specific TIMER events can be enabled, suspended, turned off or freed using TIMER(n) ON, STOP, OFF or FREE.
  • TIMER(n) FREE clears a specific timer event when it is no longer needed. The base TIMER or TIMER(0) cannot be freed!


QB64 Timing Alternatives
  • The TIMER function can be used to find timed intervals down to 1 millisecond(.001) accuracy.
  • The _DELAY statement can be used to delay program execution for intervals down to milliseconds.
  • _LIMIT can slow down loops to a specified number of frames per second. This can also alleviate a program's CPU usage.


Example: 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.


See also:




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