Question

how do I start Ttimer? timer.interval := 5000;

lbl.caption:='1'
*wait 5 sec*
lbl.caption:='2'

I have ttimer in my form and it is enabled.

Était-ce utile?

La solution

  • Set the timer's Interval property to 5000. That is the interval in milli-seconds.
  • Declare a private field of the form, FCount of type Integer.
  • 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 set Timer1.Enabled to True. Set Timer1.Enabled to False 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top