How to make one shot timer function in Delphi (like setTimeout in JavaScript)?

后端 未结 4 1903
广开言路
广开言路 2020-12-25 14:30

The setTimeout is helpful in JavaScript language. How would you create this function in delphi ?

SetTimeOut(procedure (Sender: TObject);
begin
  Self.Counter         


        
4条回答
  •  猫巷女王i
    2020-12-25 15:10

    Assuming, the function is to be called once and not 5 times every second, maybe like that:

     Parallel.Async( 
           procedure; begin
               Sleep(200);
               Self.Counter:=Self.Counter+1; end; );
    

    There are more complex solutions like the one you accepted, taking named objects for timer actions and using SetTimer method. Like http://code.google.com/p/omnithreadlibrary/source/browse/trunk/tests/17_MsgWait/test_17_MsgWait.pas Previous versions had SetTimer with anonymous function, but they are gone now.

    However for simplistic anonymous closure approach you asked for, maybe Wait(xxX) would fit.

提交回复
热议问题