OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.async; | |
6 | |
7 /** | |
8 * A count-down timer that can be configured to fire once or repeatedly. | |
9 * | |
10 * The timer counts down from the specified duration to 0. | |
11 * When the timer reaches 0, the timer invokes the specified callback function. | |
12 * Use a periodic timer to repeatedly count down the same interval. | |
13 * | |
14 * A negative duration is treated the same as a duration of 0. | |
15 * If the duration is statically known to be 0, consider using [run]. | |
16 * | |
17 * Frequently the duration is either a constant or computed as in the | |
18 * following example (taking advantage of the multiplication operator of | |
19 * the [Duration] class): | |
20 * | |
21 * const TIMEOUT = const Duration(seconds: 3); | |
22 * const ms = const Duration(milliseconds: 1); | |
23 * | |
24 * startTimeout([int milliseconds]) { | |
25 * var duration = milliseconds == null ? TIMEOUT : ms * milliseconds; | |
26 * return new Timer(duration, handleTimeout); | |
27 * } | |
28 * ... | |
29 * void handleTimeout() { // callback function | |
30 * ... | |
31 * } | |
32 * | |
33 * Note: If Dart code using Timer is compiled to JavaScript, the finest | |
34 * granularity available in the browser is 4 milliseconds. | |
35 * | |
36 * See [Stopwatch] for measuring elapsed time. | |
37 */ | |
38 abstract class Timer { | |
39 | |
40 /** | |
41 * Creates a new timer. | |
42 * | |
43 * The [callback] function is invoked after the given [duration]. | |
44 * | |
45 */ | |
46 factory Timer(Duration duration, void callback()) { | |
47 if (Zone.current == Zone.ROOT) { | |
48 // No need to bind the callback. We know that the root's timer will | |
49 // be invoked in the root zone. | |
50 return Zone.current.createTimer(duration, callback); | |
51 } | |
52 return Zone.current.createTimer( | |
53 duration, Zone.current.bindCallback(callback, runGuarded: true)); | |
54 } | |
55 | |
56 /** | |
57 * Creates a new repeating timer. | |
58 * | |
59 * The [callback] is invoked repeatedly with [duration] intervals until | |
60 * canceled with the [cancel] function. | |
61 * | |
62 * The exact timing depends on the underlying timer implementation. | |
63 * No more than `n` callbacks will be made in `duration * n` time, | |
64 * but the time between two consecutive callbacks | |
65 * can be shorter and longer than `duration`. | |
66 * | |
67 * In particular, an implementation may schedule the next callback, e.g., | |
68 * a `duration` after either when the previous callback ended, | |
69 * when the previous callback started, or when the previous callback was | |
70 * scheduled for - even if the actual callback was delayed. | |
71 */ | |
72 factory Timer.periodic(Duration duration, | |
73 void callback(Timer timer)) { | |
74 if (Zone.current == Zone.ROOT) { | |
75 // No need to bind the callback. We know that the root's timer will | |
76 // be invoked in the root zone. | |
77 return Zone.current.createPeriodicTimer(duration, callback); | |
78 } | |
79 // TODO(floitsch): the return type should be 'void', and the type | |
80 // should be inferred. | |
81 var boundCallback = Zone.current.bindUnaryCallback/*<dynamic, Timer>*/( | |
82 callback, runGuarded: true); | |
83 return Zone.current.createPeriodicTimer(duration, boundCallback); | |
84 } | |
85 | |
86 /** | |
87 * Runs the given [callback] asynchronously as soon as possible. | |
88 * | |
89 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. | |
90 */ | |
91 static void run(void callback()) { | |
92 new Timer(Duration.ZERO, callback); | |
93 } | |
94 | |
95 /** | |
96 * Cancels the timer. | |
97 */ | |
98 void cancel(); | |
99 | |
100 /** | |
101 * Returns whether the timer is still active. | |
102 * | |
103 * A non-periodic timer is active if the callback has not been executed, | |
104 * and the timer has not been canceled. | |
105 * | |
106 * A periodic timer is active if it has not been canceled. | |
107 */ | |
108 bool get isActive; | |
109 | |
110 external static Timer _createTimer(Duration duration, void callback()); | |
111 external static Timer _createPeriodicTimer(Duration duration, | |
112 void callback(Timer timer)); | |
113 } | |
114 | |
OLD | NEW |