1timer(3) Erlang Module Definition timer(3)
2
3
4
6 timer - Timer functions.
7
9 This module provides useful functions related to time. Unless otherwise
10 stated, time is always measured in milliseconds. All timer functions
11 return immediately, regardless of work done by another process.
12
13 Successful evaluations of the timer functions give return values con‐
14 taining a timer reference, denoted TRef. By using cancel/1, the re‐
15 turned reference can be used to cancel any requested action. A TRef is
16 an Erlang term, which contents must not be changed.
17
18 The time-outs are not exact, but are at least as long as requested.
19
20 Creating timers using erlang:send_after/3 and erlang:start_timer/3 is
21 more efficient than using the timers provided by this module. However,
22 the timer module has been improved in OTP 25, making it more efficient
23 and less susceptible to being overloaded. See the Timer Module section
24 in the Efficiency Guide.
25
27 time() = integer() >= 0
28
29 Time in milliseconds.
30
31 tref()
32
33 A timer reference.
34
36 apply_after(Time, Module, Function, Arguments) ->
37 {ok, TRef} | {error, Reason}
38
39 Types:
40
41 Time = time()
42 Module = module()
43 Function = atom()
44 Arguments = [term()]
45 TRef = tref()
46 Reason = term()
47
48 Evaluates apply(Module, Function, Arguments) after Time mil‐
49 liseconds.
50
51 Returns {ok, TRef} or {error, Reason}.
52
53 apply_interval(Time, Module, Function, Arguments) ->
54 {ok, TRef} | {error, Reason}
55
56 Types:
57
58 Time = time()
59 Module = module()
60 Function = atom()
61 Arguments = [term()]
62 TRef = tref()
63 Reason = term()
64
65 Evaluates apply(Module, Function, Arguments) repeatedly at in‐
66 tervals of Time.
67
68 Returns {ok, TRef} or {error, Reason}.
69
70 cancel(TRef) -> {ok, cancel} | {error, Reason}
71
72 Types:
73
74 TRef = tref()
75 Reason = term()
76
77 Cancels a previously requested time-out. TRef is a unique timer
78 reference returned by the related timer function.
79
80 Returns {ok, cancel}, or {error, Reason} when TRef is not a
81 timer reference.
82
83 exit_after(Time, Reason1) -> {ok, TRef} | {error, Reason2}
84
85 exit_after(Time, Target, Reason1) -> {ok, TRef} | {error, Reason2}
86
87 Types:
88
89 Time = time()
90 Target = pid() | (RegName :: atom())
91 TRef = tref()
92 Reason1 = Reason2 = term()
93
94 exit_after/2 is the same as exit_after(Time, self(), Reason1).
95
96 exit_after/3 sends an exit signal with reason Reason1 to Target,
97 which can be a local process identifier or an atom of a regis‐
98 tered name. Returns {ok, TRef} or {error, Reason2}.
99
100 hms(Hours, Minutes, Seconds) -> MilliSeconds
101
102 Types:
103
104 Hours = Minutes = Seconds = MilliSeconds = integer() >= 0
105
106 Returns the number of milliseconds in Hours + Minutes + Seconds.
107
108 hours(Hours) -> MilliSeconds
109
110 Types:
111
112 Hours = MilliSeconds = integer() >= 0
113
114 Returns the number of milliseconds in Hours.
115
116 kill_after(Time) -> {ok, TRef} | {error, Reason2}
117
118 kill_after(Time, Target) -> {ok, TRef} | {error, Reason2}
119
120 Types:
121
122 Time = time()
123 Target = pid() | (RegName :: atom())
124 TRef = tref()
125 Reason2 = term()
126
127 kill_after/1 is the same as exit_after(Time, self(), kill).
128
129 kill_after/2 is the same as exit_after(Time, Target, kill).
130
131 minutes(Minutes) -> MilliSeconds
132
133 Types:
134
135 Minutes = MilliSeconds = integer() >= 0
136
137 Returns the number of milliseconds in Minutes.
138
139 now_diff(T2, T1) -> Tdiff
140
141 Types:
142
143 T1 = T2 = erlang:timestamp()
144 Tdiff = integer()
145 In microseconds
146
147 Calculates the time difference Tdiff = T2 - T1 in microseconds,
148 where T1 and T2 are time-stamp tuples on the same format as re‐
149 turned from erlang:timestamp/0 or os:timestamp/0.
150
151 seconds(Seconds) -> MilliSeconds
152
153 Types:
154
155 Seconds = MilliSeconds = integer() >= 0
156
157 Returns the number of milliseconds in Seconds.
158
159 send_after(Time, Message) -> {ok, TRef} | {error, Reason}
160
161 send_after(Time, Destination, Message) ->
162 {ok, TRef} | {error, Reason}
163
164 Types:
165
166 Time = time()
167 Destination =
168 pid() |
169 (RegName :: atom()) |
170 {RegName :: atom(), Node :: node()}
171 Message = term()
172 TRef = tref()
173 Reason = term()
174
175 send_after/3:
176 Evaluates Destination ! Message after Time milliseconds.
177 (Destination can be a remote or local process identifier, an
178 atom of a registered name or a tuple {RegName, Node} for a
179 registered name at another node.)
180
181 Returns {ok, TRef} or {error, Reason}.
182
183 See also the Timer Module section in the Efficiency Guide.
184
185 send_after/2:
186 Same as send_after(Time, self(), Message).
187
188 send_interval(Time, Message) -> {ok, TRef} | {error, Reason}
189
190 send_interval(Time, Destination, Message) ->
191 {ok, TRef} | {error, Reason}
192
193 Types:
194
195 Time = time()
196 Destination =
197 pid() |
198 (RegName :: atom()) |
199 {RegName :: atom(), Node :: node()}
200 Message = term()
201 TRef = tref()
202 Reason = term()
203
204 send_interval/3:
205 Evaluates Destination ! Message repeatedly after Time mil‐
206 liseconds. (Destination can be a remote or local process
207 identifier, an atom of a registered name or a tuple {Reg‐
208 Name, Node} for a registered name at another node.)
209
210 Returns {ok, TRef} or {error, Reason}.
211
212 send_interval/2:
213 Same as send_interval(Time, self(), Message).
214
215 sleep(Time) -> ok
216
217 Types:
218
219 Time = timeout()
220
221 Suspends the process calling this function for Time milliseconds
222 and then returns ok, or suspends the process forever if Time is
223 the atom infinity. Naturally, this function does not return im‐
224 mediately.
225
226 Note:
227 Before OTP 25, timer:sleep/1 did not accept integer timeout val‐
228 ues greater than 16#ffffffff, that is, 2^32-1. Since OTP 25, ar‐
229 bitrarily high integer values are accepted.
230
231
232 start() -> ok
233
234 Starts the timer server. Normally, the server does not need to
235 be started explicitly. It is started dynamically if it is
236 needed. This is useful during development, but in a target sys‐
237 tem the server is to be started explicitly. Use configuration
238 parameters for Kernel for this.
239
240 tc(Fun) -> {Time, Value}
241
242 tc(Fun, Arguments) -> {Time, Value}
243
244 tc(Module, Function, Arguments) -> {Time, Value}
245
246 Types:
247
248 Module = module()
249 Function = atom()
250 Arguments = [term()]
251 Time = integer()
252 In microseconds
253 Value = term()
254
255 tc/3:
256 Evaluates apply(Module, Function, Arguments) and measures
257 the elapsed real time as reported by erlang:mono‐
258 tonic_time/0.
259
260 Returns {Time, Value}, where Time is the elapsed real time
261 in microseconds, and Value is what is returned from the ap‐
262 ply.
263
264 tc/2:
265 Evaluates apply(Fun, Arguments). Otherwise the same as tc/3.
266
267 tc/1:
268 Evaluates Fun(). Otherwise the same as tc/2.
269
271 Example 1
272
273 The following example shows how to print "Hello World!" in 5 seconds:
274
275 1> timer:apply_after(5000, io, format, ["~nHello World!~n", []]).
276 {ok,TRef}
277 Hello World!
278
279 Example 2
280
281 The following example shows a process performing a certain action, and
282 if this action is not completed within a certain limit, the process is
283 killed:
284
285 Pid = spawn(mod, fun, [foo, bar]),
286 %% If pid is not finished in 10 seconds, kill him
287 {ok, R} = timer:kill_after(timer:seconds(10), Pid),
288 %% We change our mind...
289 timer:cancel(R),
290
292 A timer can always be removed by calling cancel/1.
293
294 An interval timer, that is, a timer created by evaluating any of the
295 functions apply_interval/4, send_interval/3, and send_interval/2 is
296 linked to the process to which the timer performs its task.
297
298 A one-shot timer, that is, a timer created by evaluating any of the
299 functions apply_after/4, send_after/3, send_after/2, exit_after/3,
300 exit_after/2, kill_after/2, and kill_after/1 is not linked to any
301 process. Hence, such a timer is removed only when it reaches its time-
302 out, or if it is explicitly removed by a call to cancel/1.
303
304
305
306Ericsson AB stdlib 4.2 timer(3)