Difference between revisions of "ON TIMER(n)"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>SMcNeill
m (-)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The '''ON TIMER''' statement sets up a timed event to be repeated at specified intervals.
+
The [[ON_TIMER(n)|ON TIMER]] statement sets up a timed event to be repeated at specified intervals throughout a program when enabled.
  
  
<center>''Qbasic Syntax:'' ON TIMER(seconds%) GOSUB {linelabel|linenumber}</center>
+
{{PageSyntax}}
 
+
: '''ON TIMER'''({{Parameter|seconds%}}) [[GOSUB]] {{{Parameter|lineLabel}}|{{Parameter|lineNumber}}}
 
+
: '''ON TIMER'''([{{Parameter|number%}},] {{Parameter|seconds!}}) { [[SUB]]procedure | [[GOSUB]] {{{Parameter|lineLabel}}|{{Parameter|lineNumber}}} }
<center>''QB64 Syntax #1:'' ON TIMER([number%,] seconds!) GOSUB {linelabel|linenumber}</center>
 
<center>''QB64 Syntax #2:'' ON TIMER([number%,] seconds!) SubProcedure [parameter[,...]]</center>
 
  
  
 
{{PageDescription}}
 
{{PageDescription}}
<center> '''QBasic and QB64 Information''' </center>
+
===Legacy syntax===
 +
* In the first syntax, the [[INTEGER]] {{Parameter|seconds%}} parameter can be from 1 to 86400 seconds (one day).
 +
* A [[TIMER (statement)|TIMER ON]] statement must follow an '''ON TIMER''' event setup to initiate it.
 +
* [[TIMER (statement)|TIMER STOP]] disables timer events but remembers previous events when enabled again by a [[TIMER (statement)|TIMER ON]] statement, and the recorded events may be executed immediately if a timer event has occurred.
 +
* [[TIMER (statement)|TIMER OFF]] disables timer event trapping. Events will not be remembered in a subsequent [[TIMER (statement)|TIMER ON]] statement.
 +
* '''ON TIMER''' events will interrupt a [[SLEEP]] call and [[RETURN]] to running program procedures.
 +
* Only one TIMER event can be set at a time using this legacy syntax and all TIMER code must be in the main code, as it uses [[GOSUB]].
  
* In Qbasic the {{KW|INTEGER}} seconds parameter can be from 1 to 86400 seconds(one day).
 
* A {{KW|TIMER (statement)|TIMER ON}} statement must be made before an ON TIMER event is enabled in QB or QB64.
 
* {{KW|TIMER (statement)|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.
 
* {{KW|TIMER (statement)|TIMER OFF}} disables timer event trapping. Events will not be remembered in a subsequent ON TIMER statement.
 
* ON TIMER events will interrupt a {{KW|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.
 
  
<center> '''QB64 Specific Information''' </center>
+
===QB64 syntax===
 +
* '''QB64''' can use multiple numbered timer events and [[SINGLE]] floating point second values down to one millisecond (.001).
 +
* The '''TIMER''' {{Parameter|number%}} must be obtained from the [[_FREETIMER]] function. Store _FREETIMER numbers in a variable or an array to be able to reference them later.
 +
* If the '''TIMER''' number is omitted or {{InlineCode}}'''ON TIMER'''(0, {{Parameter|seconds!}}){{InlineCodeEnd}} is used, then the TIMER used is the ''base TIMER'' (same as in the legacy syntax above).
 +
* [[SUB]] procedures are allowed to be referenced, but [[CALL]] must not be used.
 +
* '''[[SUB]] parameter values are passed by value and should be [[SHARED]] or literal values.'''
 +
* Specific '''TIMER''' events can be turned on, suspended, turned off or freed using [[TIMER (statement)|TIMER(n)]] ON, STOP, OFF or FREE.
 +
* Use '''TIMER(n) FREE''' to release a timer event after it has been turned off or is no longer used.
 +
** The ''base TIMER'' cannot be freed.
 +
* '''QB64''' allows TIMER statements to also be inside of SUB and FUNCTION procedures.
 +
* '''ON TIMER''' events will interrupt a [[SLEEP]] call and [[RETURN]] to running program procedures.
 +
* [[$CHECKING]]:OFF can disable all QB64 event checking. '''Setting $CHECKING:OFF is only designed for 100% stable, error-less sections of code, where every CPU cycle saved counts.'''
  
* Can use multiple timers and {{KW|SINGLE}} floating point second values down to one millisecond(.001).
 
* If the TIMER number is omitted, the TIMER used is the base timer. That TIMER cannot be freed using TIMER[0] FREE. No number referance is used for that timer and keeps QB64 compatible with Qbasic.
 
* Direct {{KW|SUB}} procedure calls are also allowed without using {{KW|CALL}}.
 
* Use the {{KW|_FREETIMER}} function to save free timer numbers to variables for TIMER statements.
 
* TIMER(n) FREE releases a timer event after being turned off.
 
* '''QB64''' allows TIMER statements to also be inside of SUB and FUNCTION procedures.
 
* Does not currently interrupt {{KW|SLEEP}} calls.
 
* {{KW|$CHECKING}}:OFF can disable QB64 event checking. '''It should ONLY be used with errorless code that needs every CPU cycle!'''
 
  
 +
==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.
  
  
 +
{{PageExamples}}
 +
''Example:'' Using a numbered TIMER to check the mouse button press status in '''QB64'''.
 +
{{CodeStart}} '' ''
 +
{{Cl|DIM}} {{Cl|SHARED}} Button {{Cl|AS}} {{Cl|LONG}}    'share variable value with Sub
  
''Example:'' How to update the time while {{KW|PRINT|printing}} at the same time in a program.
+
t1 = _{{Cl|TIMER (statement)|FREE}}{{Cl|TIMER}}              'get a timer number from _FREETIMER ONLY!
 +
{{Cl|ON TIMER(n)|ON TIMER}}(t1, .05) MouseClick
 +
{{Cl|TIMER}}(t1) ON
  
{{CodeStart}}
+
DO
   TIMER ON ' enable timer event trapping
+
  {{Cl|LOCATE}} 1, 1
  LOCATE 4, 2 ' set the starting PRINT position
+
   {{Cl|IF...THEN|IF}} Button {{Cl|THEN}}
   {{Cl|ON TIMER(n)|ON TIMER}}(10) GOSUB Clock ' set procedure execution repeat time
+
    {{Cl|PRINT}} "Mouse button"; Button; "is pressed.";
   DO WHILE INKEY$ = "": PRINT "A"; : SLEEP 6: LOOP
+
   {{Cl|ELSE}} {{Cl|PRINT}} {{Cl|SPACE$}}(70)
  TIMER OFF
+
  {{Cl|END IF}}
  {{Cl|SYSTEM}}
+
   {{Cl|_DISPLAY}}
 +
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
 +
{{Cl|TIMER}}(t1) {{Cl|OFF}}
 +
{{Cl|TIMER}}(t1) {{Cl|TIMER (statement)|FREE}} 'release timer
 +
{{Cl|END}}
  
Clock:
+
{{Cl|SUB}} MouseClick
  row = {{Cl|CSRLIN}} ' Save current print cursor row.
+
{{Cl|DO...LOOP|DO}} {{Cl|WHILE}} {{Cl|_MOUSEINPUT}}
   col = {{Cl|POS|POS(0)}} ' Save current print cursor column.
+
   {{Cl|IF...THEN|IF}} {{Cl|_MOUSEBUTTON}}(1) {{Cl|THEN}}
   LOCATE 2, 37: PRINT {{Cl|TIME$}}; ' print current time at top of screen.
+
    {{Cl|COLOR}} 10: Button = 1
   LOCATE row, col ' return to last print cursor position
+
   {{Cl|ELSEIF}} {{Cl|_MOUSEBUTTON}}(2) {{Cl|THEN}}
{{Cl|RETURN}}
+
    {{Cl|COLOR}} 12: Button = 2
 +
  {{Cl|ELSE}} Button = 0
 +
   {{Cl|END IF}}
 +
{{Cl|LOOP}}
 +
{{Cl|END SUB}} '' ''
 
{{CodeEnd}}
 
{{CodeEnd}}
 
: NOTE: SLEEP will be interrupted in Qbasic.
 
 
  
  
 
{{PageSeeAlso}}
 
{{PageSeeAlso}}
 
+
* [[TIMER]], [[_FREETIMER]]
* {{KW|TIMER}}, {{KW|_FREETIMER}}
+
* [[TIMER (statement)]], [[_DELAY]], [[_LIMIT]]
 
+
* [[$CHECKING]] {{text|(QB64 [[Metacommand]])}}
* {{KW|TIMER (statement)}}, {{KW|_DELAY}}, {{KW|_LIMIT}}
 
 
 
* {{KW|$CHECKING}} (QB64 C++ {{KW|Metacommand}})
 
 
 
 
 
  
  
 
{{PageNavigation}}
 
{{PageNavigation}}

Latest revision as of 12:42, 16 October 2017

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


Syntax

ON TIMER(seconds%) GOSUB {lineLabel|lineNumber}
ON TIMER([number%,] seconds!) { SUBprocedure | GOSUB {lineLabel|lineNumber} }


Description

Legacy syntax

  • In the first syntax, the INTEGER seconds% parameter can be from 1 to 86400 seconds (one day).
  • A TIMER ON statement must follow an ON TIMER event setup to initiate it.
  • TIMER STOP disables timer events but remembers previous events when enabled again by a TIMER ON statement, and the recorded events may be executed immediately if a timer event has occurred.
  • TIMER OFF disables timer event trapping. Events will not be remembered in a subsequent TIMER ON statement.
  • ON TIMER events will interrupt a SLEEP call and RETURN to running program procedures.
  • Only one TIMER event can be set at a time using this legacy syntax and all TIMER code must be in the main code, as it uses GOSUB.


QB64 syntax

  • QB64 can use multiple numbered timer events and SINGLE floating point second values down to one millisecond (.001).
  • The TIMER number% must be obtained from the _FREETIMER function. Store _FREETIMER numbers in a variable or an array to be able to reference them later.
  • If the TIMER number is omitted or ON TIMER(0, seconds!) is used, then the TIMER used is the base TIMER (same as in the legacy syntax above).
  • SUB procedures are allowed to be referenced, but CALL must not be used.
  • SUB parameter values are passed by value and should be SHARED or literal values.
  • Specific TIMER events can be turned on, suspended, turned off or freed using TIMER(n) ON, STOP, OFF or FREE.
  • Use TIMER(n) FREE to release a timer event after it has been turned off or is no longer used.
    • The base TIMER cannot be freed.
  • QB64 allows TIMER statements to also be inside of SUB and FUNCTION procedures.
  • ON TIMER events will interrupt a SLEEP call and RETURN to running program procedures.
  • $CHECKING:OFF can disable all QB64 event checking. Setting $CHECKING:OFF is only designed for 100% stable, error-less sections of code, where every CPU cycle saved counts.


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.


Examples

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