Recent

Author Topic: How to make a simple countdown timer?  (Read 3300 times)

Lampbert

  • New Member
  • *
  • Posts: 33
How to make a simple countdown timer?
« on: January 26, 2020, 04:34:31 pm »
How do you create a simple countdown timer (dynamically, without using a timer added to the form in the designer) and have it count down in the format hh:nn:ss?

I've tried starting it off, but to be honest I have no idea where to go from here:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CreateTimer;
  2. var
  3.   NewTimer: TTimer;
  4. begin
  5.   NewTimer := TTimer.Create(Self);
  6.   with NewTimer do
  7.   begin
  8.     ;
  9.     Interval := 1000;
  10.     Enabled := True;
  11.   end;
  12. end;  

Thank you for any help

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: How to make a simple countdown timer?
« Reply #1 on: January 26, 2020, 04:58:46 pm »
Hello Lampbert,
Welcome to the forum.

Is it a school assignment? Never mind.

You should declare NewTimer as a global variable.

Lampbert

  • New Member
  • *
  • Posts: 33
Re: How to make a simple countdown timer?
« Reply #2 on: January 26, 2020, 05:19:13 pm »
Hello Lampbert,
Welcome to the forum.

Is it a school assignment? Never mind.

You should declare NewTimer as a global variable.
Thanks for reply. It's not a school assingment, it's for a beginner project I started about a week ago, it's just I didn't anticipate that this part would be so confusing. You're right, it should be global variable especially since I have multiple forms, thanks.
« Last Edit: January 26, 2020, 05:21:22 pm by Lampbert »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to make a simple countdown timer?
« Reply #3 on: January 26, 2020, 05:44:45 pm »
I've tried starting it off, but to be honest I have no idea where to go from here:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CreateTimer;
  2. var
  3.   NewTimer: TTimer;
  4. begin
  5.   NewTimer := TTimer.Create(Self);
  6.   with NewTimer do
  7.   begin
  8.     ;
  9.     Interval := 1000;
  10.     Enabled := True;
  11.   end;
  12. end;  

Before activating the timer, you should set a handler for the OnTimer event; otherwise the only thing your timer does is to waste resources  ;D

Code: Pascal  [Select][+][-]
  1. procedure TForm1.TimerTimeout(Sender: TObject);
  2. begin
  3.   {Do whatever when the timer shots an OnTimer event}
  4. end;
  5.  
  6. procedure TForm1.CreateTimer;
  7. {Declared as form field or global: NewTimer: TTimer}
  8. begin
  9.   if not Assigned(NewTimer) then begin
  10.     NewTimer := TTimer.Create(Self)
  11.     with NewTimer do
  12.     begin
  13.       Interval := 1000;
  14.       OnTimer := @TimerTimeout
  15.       Enabled := True;
  16.     end;
  17.   end;
  18. end;

Now all you have to do is to fill up the handler (TimerTimeout) to do whatever you want: update a label showing the time, invoking a temporal file update proc, etc.

HTH


ETA: I have a very simple "clock" application which demonstrates how to use a TTimer; I'll post it if you want but only if this is not for school work (copy-paste homework is very bad for your intellect!)
« Last Edit: January 26, 2020, 05:56:06 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Lampbert

  • New Member
  • *
  • Posts: 33
Re: How to make a simple countdown timer?
« Reply #4 on: January 27, 2020, 12:13:06 am »
Cheers for that lucamar. I promise I'm not doing it for a homework assignment, it's just for a personal project :D.

Your code works great for creating the timer, now I've just been watching some YouTube tutorials on how TTimer works so I've made a little progress on making a countdown timer:

Code: Pascal  [Select][+][-]
  1. var
  2.   Form2: TForm2;
  3.   NewTimer: TTimer;
  4.   Time: integer = 0;
  5.  
  6. procedure TForm2.CreateTimer;
  7. begin
  8.   if not Assigned(NewTimer) then begin
  9.     NewTimer := TTimer.Create(Self);
  10.     with NewTimer do
  11.     begin
  12.       Interval := 1000;
  13.       OnTimer := @CountdownTimer;
  14.       Enabled := True;
  15.     end;
  16.   end;
  17. end;
  18.  
  19. procedure TForm2.CountdownTimer(Sender: TObject);
  20. begin
  21.   Time := Time - 1;
  22.   begin
  23.     Form1.Label1.Caption := IntToStr(Time);
  24.   end
  25. end;  

I am still struggling to think of a way to make it count down like:

Code: Pascal  [Select][+][-]
  1. 8:00:00
  2. 7:59:59
  3. 7:59:58, etc... (hh:nn:ss)

I've had a look at a list of functions on the forum and I think FormatDateTime is what I need, but the timer only works with integers, right? So I don't know how that would work.

So I would love to see your clock application to see if it sheds any more light on this.
« Last Edit: January 27, 2020, 12:37:52 am by Lampbert »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to make a simple countdown timer?
« Reply #5 on: January 27, 2020, 02:11:56 am »
Hi!

The TDateTime format is a double.
The fractional  part is the part of the day, so 0.5 = 12 hours.

Change this: change the name of  'time' because time is a procedure in sysutils.
And change it to a double.
So:

Code: Pascal  [Select][+][-]
  1. var
  2. MyTime :  double = 1; // 24 hours
  3.  
  4. ....
  5.  
  6. procedure TForm2.CountdownTimer(Sender: TObject);
  7. begin
  8.   myTime := myTime - 1/(24*60*60);
  9.   Form1.Label1.Caption := TimeToStr(MyTime);
  10. end;  
  11.  
  12.  
  13.  

Happy Countdown!

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to make a simple countdown timer?
« Reply #6 on: January 27, 2020, 03:04:28 pm »
I am still struggling to think of a way to make it count down like:

Code: Pascal  [Select][+][-]
  1. 8:00:00
  2. 7:59:59
  3. 7:59:58, etc... (hh:nn:ss)

I've had a look at a list of functions on the forum and I think FormatDateTime is what I need, but the timer only works with integers, right? So I don't know how that would work.

Making it count down is relartively simple: get from the user or calculate the end-time and show (with TimeToStr(), FormatDateTime(), etc.) the difference of: TargetTime-Time, as in, for example:

Code: Pascal  [Select][+][-]
  1. procedure Form1.CountdownTick(Sender: TObject);
  2. {Uses Target:TDateTime (or Taget: TTime) declared and set elsewhere}
  3. var
  4.   TimeToTarget: TDateTime;
  5. begin
  6.   if Time > Target then begin
  7.     Timer1.Enabled := False;
  8.     { Do whatever else you want down when the countdown reaches zero }
  9.   end else begin
  10.     TimeToTarget := Target - Time;
  11.     Label1.Caption := TimeToStr(TimeToTarget);
  12.   end;
  13. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018