Lessons 61–62 · 2 lessons · N. I. Taylaqov (general editor), A. B. Akhmedov, M. D. Pardayeva, A. A. Abdug‘aniyev, U. M. Mirsanov. Informatics and Information Technologies, Grade 10, 1st edition. “Extremum-Press”, Tashkent, 2017
61
The Timer object and how to use it
Textbook: pp. 138–143
GoalUses the Interval and Enabled properties and the OnTimer event of the Timer object: creates an electronic clock, a countdown and a stop/start button.
New words
Timer: an invisible object that triggers an event at regular intervals · TimerInterval: the time between two timer events, in milliseconds · IntervalOnTimer: the event that runs each time the interval passes · OnTimermillisecond: one thousandth of a second · millisekund
Explanation
For time-dependent jobs – a clock, a countdown, animation – the Timer (System tab) is used. It is not visible in the application window, but if it is enabled, it triggers the OnTimer event after the set time, the procedure runs and the application continues its work. The Timer has two properties: Enabled (it works if True and stops if False; True at first) and Interval – the time between events in milliseconds (1000 at first, that is 1 second). 1 second = 1000 milliseconds, so for it to run 20 times a second Interval = 1000 / 20 = 50. For a clock write Label1.Caption := TimeToStr(Now); Now is the current time of the computer clock and TimeToStr converts it to text. A countdown needs a global variable, because the value must be kept from one run of OnTimer to the next; do not give it a property name such as Left, Top, Width or Caption, or inside a form procedure that name will mean the form’s own property. The timer interval is not exact: Windows can delay it slightly, so if exact time is needed, use Now instead of counting ticks.
Worked examples
A clock: Label1 (Font.Size = 36), Timer1 (Interval = 1000). procedure TForm1.Timer1Timer(Sender: TObject); ⏎ begin ⏎ Label1.Caption := TimeToStr(Now); ⏎ end; The text is updated every second, for example from 09:15:07 to 09:15:08.
A countdown (a race start): global var qoldi: Integer; The Ready button: qoldi := 10; Timer1.Enabled := True; Timer1 (Interval = 1000): procedure TForm1.Timer1Timer(Sender: TObject); ⏎ begin ⏎ qoldi := qoldi - 1; ⏎ if qoldi > 0 then ⏎ Label1.Caption := IntToStr(qoldi) ⏎ else ⏎ begin ⏎ Label1.Caption := 'Start!'; ⏎ Timer1.Enabled := False; ⏎ end; ⏎ end; The Label shows 9, 8, …, 1 and then Start! appears. With Interval = 1000 it takes 10 seconds, and with Interval = 500 it takes 10·500 ms = 5 seconds.
Class activity
“The count”: a pair builds a 20-second countdown application, adds a “Stop/Continue” button (Enabled := not Enabled) and calculates in advance how the counting time changes when Interval is changed.
Practice
1
In which unit is the Interval property of a Timer measured?
Milliseconds
2
If Interval = 250, how many times does the Timer run in 1 second?
4
3
A countdown starts at 45 with Interval = 1000. In how many seconds does it reach 0?
45
4
Why is the countdown value kept in a global variable?
A local variable disappears when the procedure ends, but the value must be kept until the next run of OnTimer.