- Set the timer's Interval property to
5000
. That is the interval in milli-seconds. - Declare a private field of the form,
FCount
of typeInteger
. - Attach on OnTimer event handler to the timer:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
inc(FCount);
lbl.Caption := IntToStr(FCount);
end;
- When you want to start the timer, initialise
FCount
and setTimer1.Enabled
toTrue
. SetTimer1.Enabled
toFalse
when you want to stop the timer.
I am assuming, based on your now deleted earlier question, that you want the counter to keep ticking.
Timers work by calling their OnTimer
event every time they tick. Because each tick is a distinct call to the event handler, you have to store any persistent state somewhere other than local variables. That's because local variables only endure for the duration of their owning method. Hence the use of a private field of the form to maintain the count.
Note that a timer does not make the program wait. Timers are asynchronous. The program will still be responsive to user input while the timer is active. When the timer expires, the system synthesises a timer message in your message queue. The program does not block on the timer. So your UI elements will, unless you take action otherwise, still be enabled and responsive.