Timer problem: unneeded OnTimer call lazarus / 31-LCL ---------------------------------------------------------------------- AlexTP · 2022-09-01 19:29 I test this on Linux x64 (gtk2 and qt5). Demo attached. What app must do. When I start typing (e.g. I hold 'd') in TEdit, timer starts, and on each TEdit.OnChange timer is disabled+enabled. I expect to see OnTimer call only when I release the pressed key. But: I see OnTimer call also when I press the key. It is unneeded for me. I want to receive the OnTimer in buffered way - if I type faster than Timer1.Interval I want to get only the LAST OnTimer. Not all intermediate OnTimer calls. Demo supports this - it does in TEdit.OnChange Timer1.Enabled:= false; Timer1.Enabled:= true; Lazarus 2.3.0 (rev main-2_3-1386-g23b2324f9f) FPC 3.2.3 x86_64-linux- gtk2 ---------------------------------------------------------------------- rvk · 2022-09-01 19:36 Quote from: AlexTP on September 01, 2022, 09:29:39 pmI want OnTimer call only on releasing the key. Why not put this in TEdit.OnKeyUp in that case? ---------------------------------------------------------------------- AlexTP · 2022-09-01 19:38 Because the final app will use OnCaretPosChanged in ATSynEdit, while in demo I made the test for OnChange, was easier to demonstate the problem. ---------------------------------------------------------------------- rvk · 2022-09-01 19:43 Quote from: AlexTP on September 01, 2022, 09:38:48 pmBecause the final app will use OnCaretPosChanged in ATSynEdit, while in demo I made the test for OnChange, was easier to demonstate the problem. Then you need to explain what you want. If you want the timer to activate when releasing a key, you should use the event for releasing the key (i.e. OnKeyUp). The OnChange is triggered on every change (so holding the key will trigger it multiple times). ---------------------------------------------------------------------- AlexTP · 2022-09-01 19:46 I want to receive the OnTimer in buffered way - if I type faster than Timer.Interval I want to get only the LAST OnTimer. Not all intermediate OnTimer calls. Demo supports this - it does in TEdit.OnChange Timer1.Enabled:= false; Timer1.Enabled:= true; ---------------------------------------------------------------------- rvk · 2022-09-01 19:53 I still don't understand. Do you want the timer event on the keydown, keyup, onchange or something else? You can also enable the timer in the OnChange and disable the timer in the TTimer.OnTimer event. In that case you will only get one timer event. But you need to look at when you want to enable it again. What is the idea behind the timer? ---------------------------------------------------------------------- AlexTP · 2022-09-01 20:06 The idea is - fire the app API event after "caret pos is changed" (TEdit.OnChange is not the same - it was only to show the problem). but not immediately, only after a pause! ---------------------------------------------------------------------- rvk · 2022-09-01 20:12 So enable it on an event of your choice (only when it's disabled) and disable it in the timer event. ---------------------------------------------------------------------- KodeZwerg · 2022-09-01 20:45 Quote from: AlexTP on September 01, 2022, 10:06:09 pmThe idea is - fire the app API event after "caret pos is changed" (TEdit.OnChange is not the same - it was only to show the problem). but not immediately, only after a pause! Using the OnChange event is a good place to activate your TTimer and to deactivate simple do it inside the TTimer event itself, else you have a logical problem. ---------------------------------------------------------------------- AlexTP · 2022-09-01 20:53 Quote from: KodeZwerg Using the OnChange event is a good place to activate your TTimer and to deactivate simple do it inside the TTimer event itself, else you have a logical problem. Demo already does it! Problem is not here. ---------------------------------------------------------------------- rvk · 2022-09-01 21:02 Quote from: AlexTP on September 01, 2022, 10:53:13 pmQuote from: KodeZwerg Using the OnChange event is a good place to activate your TTimer and to deactivate simple do it inside the TTimer event itself, else you have a logical problem. Demo already does it! Problem is not here. You can't use OnChange for enabling the timer without firing it multiple times. If you really want to do that you need to make sure not to enable it again. For example. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1Change(Sender: TObject);begin  if Timer1.Tag = 0 then  begin    Timer1.Interval:= 400;    Timer1.Enabled:= true;    Timer1.Tag := 1;  endend; Then the timer only triggers once. But when do you want to trigger it again? (You'll need to set the Tag to 0 then) ---------------------------------------------------------------------- AlexTP · 2022-09-01 21:28 no no no. I need to fire Timer once __per each block of editings__, ie __per one holding of letter-key__. So I hold 'd' for 3 seconds, and I need to fire Timer once for these 3 seconds. But problem- it's fired TWICE. ---------------------------------------------------------------------- rvk · 2022-09-01 21:55 Quote from: AlexTP on September 01, 2022, 11:28:51 pmno no no. I need to fire Timer once __per each block of editings__, ie __per one holding of letter-key__. So I hold 'd' for 3 seconds, and I need to fire Timer once for these 3 seconds. But problem- it's fired TWICE. That's what the OnKeyUp is for. It's only fired once when the key is released. With the tag method in OnKeyDown the timer should only fire once and you can reset tag to 0 in the OnKeyUp event. ---------------------------------------------------------------------- AlexTP · 2022-09-02 05:14 OnKeyUp will fix it in this demo, but it will not fix timer issue in my map app. For the main app, I use this timer for different event (OnChangeCaretPos). ---------------------------------------------------------------------- rvk · 2022-09-02 06:03 Quote from: AlexTP on September 02, 2022, 07:14:10 amOnKeyUp will fix it in this demo, but it will not fix timer issue in my map app. For the main app, I use this timer for different event (OnChangeCaretPos). And why not use OnKeyUp if it fixes the problem you described? Otherwise you need to show the real problem with the actual component. ---------------------------------------------------------------------- ojz0r · 2022-09-02 06:31 Quote from: AlexTP on September 01, 2022, 11:28:51 pmno no no. I need to fire Timer once __per each block of editings__, ie __per one holding of letter-key__. So I hold 'd' for 3 seconds, and I need to fire Timer once for these 3 seconds. But problem- it's fired TWICE. Can't you use OnKeyDown and not Timer1.Enabled as condition? ---------------------------------------------------------------------- rvk · 2022-09-02 06:41 Quote from: ojz0r on September 02, 2022, 08:31:20 amQuote from: AlexTP on September 01, 2022, 11:28:51 pmno no no. I need to fire Timer once __per each block of editings__, ie __per one holding of letter-key__. So I hold 'd' for 3 seconds, and I need to fire Timer once for these 3 seconds. But problem- it's fired TWICE. Can't you use OnKeyDown and not Timer1.Enabled as condition? You can't enable TTimer.enabled in OnKeyDown by checking TTimer.Enabled itself because it is triggered multiple times so you'll never know when to actually enable it (because it's also disabled again in the timer). You'll need an extra flag to indicate it's already executed before deciding you can switch the enabled. But there is already a solution for the problem described (OnKeyUp). So we need a new correct problem description with a proper example showing that problem.