ltimer.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. { lNet Timer
  2. CopyRight (C) 2006-2008 Micha Nelissen
  3. This library is Free software; you can rediStribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. This license has been modified. See File LICENSE.ADDON for more inFormation.
  15. Should you find these sources without a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit ltimer;
  19. {$mode objfpc}{$H+}
  20. interface
  21. uses
  22. Classes, SysUtils;
  23. type
  24. { TLTimer }
  25. TLTimer = class(TObject)
  26. protected
  27. FOnTimer: TNotifyEvent;
  28. FInterval: TDateTime;
  29. FStarted: TDateTime;
  30. FOneShot: Boolean;
  31. FEnabled: Boolean;
  32. function GetInterval: Integer;
  33. procedure SetInterval(const aValue: Integer);
  34. public
  35. procedure CallAction;
  36. property Enabled: Boolean read FEnabled write FEnabled;
  37. property Interval: Integer read GetInterval write SetInterval;
  38. property OneShot: Boolean read FOneShot write FOneShot;
  39. property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;
  40. end;
  41. implementation
  42. { TLTimer }
  43. function TLTimer.GetInterval: Integer;
  44. begin
  45. Result := Round(FInterval * MSecsPerDay);
  46. end;
  47. procedure TLTimer.SetInterval(const aValue: Integer);
  48. begin
  49. FInterval := AValue / MSecsPerDay;
  50. FStarted := Now;
  51. FEnabled := true;
  52. end;
  53. procedure TLTimer.CallAction;
  54. begin
  55. if FEnabled and Assigned(FOnTimer) and (Now - FStarted >= FInterval) then
  56. begin
  57. FOnTimer(Self);
  58. if not FOneShot then
  59. FStarted := Now
  60. else
  61. FEnabled := false;
  62. end;
  63. end;
  64. end.