2727% %-----------------------------------------------------------------------------
2828-module (timer ).
2929
30- -export ([sleep /1 ]).
30+ -export ([sleep /1 , send_after / 2 , send_after / 3 , apply_after / 4 ]).
3131
3232% %-----------------------------------------------------------------------------
3333% % @param Timeout number of milliseconds to sleep or `infinity'
@@ -43,3 +43,53 @@ sleep(Timeout) ->
4343 after Timeout ->
4444 ok
4545 end .
46+
47+ % %-----------------------------------------------------------------------------
48+ % % @param Time time in milliseconds after which to send the message.
49+ % % @param Message the message to send to the calling process.
50+ % % @returns `{ok, TRef}' where `TRef' is a reference to the timer.
51+ % %
52+ % % @doc Sends `Message' to the calling process after `Time' milliseconds.
53+ % % @end
54+ % %-----------------------------------------------------------------------------
55+ -spec send_after (Time :: non_neg_integer (), Message :: term ()) -> {ok , reference ()}.
56+ send_after (Time , Message ) ->
57+ TRef = timer_manager :send_after (Time , self (), Message ),
58+ {ok , TRef }.
59+
60+ % %-----------------------------------------------------------------------------
61+ % % @param Time time in milliseconds after which to send the message.
62+ % % @param Pid the process to which to send the message.
63+ % % @param Message the message to send.
64+ % % @returns `{ok, TRef}' where `TRef' is a reference to the timer.
65+ % %
66+ % % @doc Sends `Message' to `Pid' after `Time' milliseconds.
67+ % % @end
68+ % %-----------------------------------------------------------------------------
69+ -spec send_after (Time :: non_neg_integer (), Pid :: pid () | atom (), Message :: term ()) ->
70+ {ok , reference ()}.
71+ send_after (Time , Pid , Message ) ->
72+ TRef = timer_manager :send_after (Time , Pid , Message ),
73+ {ok , TRef }.
74+
75+ % %-----------------------------------------------------------------------------
76+ % % @param Time time in milliseconds after which to apply the function.
77+ % % @param Module the module of the function to apply.
78+ % % @param Function the function to apply.
79+ % % @param Arguments the arguments to pass to the function.
80+ % % @returns `{ok, TRef}' where `TRef' is a reference to the timer.
81+ % %
82+ % % @doc Applies `Module:Function(Arguments)' after `Time' milliseconds.
83+ % % @end
84+ % %-----------------------------------------------------------------------------
85+ -spec apply_after (
86+ Time :: non_neg_integer (), Module :: module (), Function :: atom (), Arguments :: [term ()]
87+ ) -> {ok , reference ()}.
88+ apply_after (Time , Module , Function , Arguments ) ->
89+ Pid = spawn (fun () ->
90+ receive
91+ apply_now -> apply (Module , Function , Arguments )
92+ end
93+ end ),
94+ TRef = timer_manager :send_after (Time , Pid , apply_now ),
95+ {ok , TRef }.
0 commit comments