ON TIMER(n): Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
{{PageDescription}} | {{PageDescription}} | ||
=== Legacy syntax === | === Legacy syntax === | ||
* In the first syntax, the [[INTEGER]] {{Parameter|seconds%}} parameter can be from 1 to 86400 seconds (one day). | * In the first syntax, the [[INTEGER]] {{Parameter|seconds%}} parameter can be from 1 to 86400 seconds (one day). | ||
* A [[TIMER | * A [[TIMER|TIMER ON]] statement must follow an '''ON TIMER''' event setup to initiate it. | ||
* [[TIMER | * [[TIMER|TIMER STOP]] disables timer events but remembers previous events when enabled again by a [[TIMER|TIMER ON]] statement, and the recorded events may be executed immediately if a timer event has occurred. | ||
* [[TIMER | * [[TIMER|TIMER OFF]] disables timer event trapping. Events will not be remembered in a subsequent [[TIMER|TIMER ON]] statement. | ||
* '''ON TIMER''' events will interrupt a [[SLEEP]] call and [[RETURN]] to running program procedures. | * '''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]]. | * 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]]. | ||
Line 22: | Line 23: | ||
* [[SUB]] procedures are allowed to be referenced, but [[CALL]] must not be used. | * [[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.''' | * '''[[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 | * Specific '''TIMER''' events can be turned on, suspended, turned off or freed using [[TIMER|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. | * 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. | ** The ''base TIMER'' cannot be freed. | ||
Line 31: | Line 32: | ||
== QB64 Timing Alternatives == | == QB64 Timing Alternatives == | ||
* The [[TIMER]] | * 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. | * 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. | * [[_LIMIT]] can slow down loops to a specified number of frames per second. This can also alleviate a program's CPU usage. | ||
Line 41: | Line 42: | ||
{{Cl|DIM}} {{Cl|SHARED}} Button {{Cl|AS}} {{Cl|LONG}} 'share variable value with Sub | {{Cl|DIM}} {{Cl|SHARED}} Button {{Cl|AS}} {{Cl|LONG}} 'share variable value with Sub | ||
t1 = | t1 = {{Cl|_FREETIMER}} 'get a timer number from _FREETIMER ONLY! | ||
{{Cl|ON TIMER(n)|ON TIMER}}(t1, .05) MouseClick | {{Cl|ON TIMER(n)|ON TIMER}}(t1, .05) MouseClick | ||
{{Cl|TIMER}}(t1) ON | {{Cl|TIMER}}(t1) ON | ||
Line 54: | Line 55: | ||
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) | {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) | ||
{{Cl|TIMER}}(t1) {{Cl|OFF}} | {{Cl|TIMER}}(t1) {{Cl|OFF}} | ||
{{Cl|TIMER}}(t1) | {{Cl|TIMER}}(t1) FREE 'release timer | ||
{{Cl|END}} | {{Cl|END}} | ||
Line 71: | Line 72: | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[ | * [[_FREETIMER]], [[TIMER]] | ||
* | * [[_DELAY]], [[_LIMIT]] | ||
* [[$CHECKING]] | * [[$CHECKING]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 16:59, 24 February 2023
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