TIMER - mkilgore/QB64pe GitHub Wiki
The TIMER function returns the number of seconds past the previous midnite down to an accuracy of 1/18th of a second.
- seconds! = TIMER
- seconds# = TIMER[(accuracy!)]
- TIMER return values range from 0 at midnight to 86399! A comparison value must stay within that range!
- INTEGER or LONG second values range from 0 at midnight to 86399 seconds each day.
- QBasic can return SINGLE values down to about .04 or 1/18th (one tick) of a second accurately.
- QB64 can read DOUBLE accuracy down to 1 millisecond. Example: start# = TIMER(.001)
- Use DOUBLE variables for millisecond accuracy as SINGLE values are only accurate to 100ths of a second later in the day!
- TIMER loops should use a midnight adjustment to avoid non-ending loops in QBasic.
- TIMER can also be used for timing program Events. See ON TIMER(n) or the TIMER (statement)
- QB64 can use a _DELAY down to .001(one millisecond) or _LIMIT loops per second. Both help limit program CPU usage.
END SUB Delay (dlay!) start! = TIMER DO...LOOP WHILE start! + dlay! >= TIMER IF...THEN start! > TIMER THEN start! = start! - 86400 LOOP END SUB '' '' |
- Explanation: When the delay time is added to the present TIMER value, it could be over the maximum number of 86399 seconds. So when TIMER becomes less than start it has reached midnight. The delay value then must be corrected by subtracting 86400.
END '' '' |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 code 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 code 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 code |
- Explanation: The POKE before the delay loop sets the tick count to 0. The PEEK count increases until the tick count returns 18 ticks and ends the loop. The same thing could be approximated by using a delay loop with: second! = TIMER + 1
PRINT "Single ="; ts! PRINT "Double ="; td# '' '' |
Single = 77073.09 Double = 77073.094 |
- Explanation: SINGLE variables will cut off the millisecond accuracy returned so DOUBLE variables should be used. TIMER values will also exceed INTEGER limits. When displaying TIMER values, use LONG for seconds and DOUBLE for milliseconds.
- _DELAY, _LIMIT, SLEEP
- RANDOMIZE, Scancodes(example)
- ON TIMER(n), TIMER (statement)
Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page