OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 /** | 7 /** |
8 * A count-down timer that can be configured to fire once or repeatedly. | 8 * A count-down timer that can be configured to fire once or repeatedly. |
9 * | 9 * |
10 * The timer counts down from the specified duration to 0. | 10 * The timer counts down from the specified duration to 0. |
(...skipping 18 matching lines...) Expand all Loading... |
29 * void handleTimeout() { // callback function | 29 * void handleTimeout() { // callback function |
30 * ... | 30 * ... |
31 * } | 31 * } |
32 * | 32 * |
33 * Note: If Dart code using Timer is compiled to JavaScript, the finest | 33 * Note: If Dart code using Timer is compiled to JavaScript, the finest |
34 * granularity available in the browser is 4 milliseconds. | 34 * granularity available in the browser is 4 milliseconds. |
35 * | 35 * |
36 * See [Stopwatch] for measuring elapsed time. | 36 * See [Stopwatch] for measuring elapsed time. |
37 */ | 37 */ |
38 abstract class Timer { | 38 abstract class Timer { |
39 | |
40 /** | 39 /** |
41 * Creates a new timer. | 40 * Creates a new timer. |
42 * | 41 * |
43 * The [callback] function is invoked after the given [duration]. | 42 * The [callback] function is invoked after the given [duration]. |
44 * | 43 * |
45 */ | 44 */ |
46 factory Timer(Duration duration, void callback()) { | 45 factory Timer(Duration duration, void callback()) { |
47 if (Zone.current == Zone.ROOT) { | 46 if (Zone.current == Zone.ROOT) { |
48 // No need to bind the callback. We know that the root's timer will | 47 // No need to bind the callback. We know that the root's timer will |
49 // be invoked in the root zone. | 48 // be invoked in the root zone. |
(...skipping 12 matching lines...) Expand all Loading... |
62 * The exact timing depends on the underlying timer implementation. | 61 * The exact timing depends on the underlying timer implementation. |
63 * No more than `n` callbacks will be made in `duration * n` time, | 62 * No more than `n` callbacks will be made in `duration * n` time, |
64 * but the time between two consecutive callbacks | 63 * but the time between two consecutive callbacks |
65 * can be shorter and longer than `duration`. | 64 * can be shorter and longer than `duration`. |
66 * | 65 * |
67 * In particular, an implementation may schedule the next callback, e.g., | 66 * In particular, an implementation may schedule the next callback, e.g., |
68 * a `duration` after either when the previous callback ended, | 67 * a `duration` after either when the previous callback ended, |
69 * when the previous callback started, or when the previous callback was | 68 * when the previous callback started, or when the previous callback was |
70 * scheduled for - even if the actual callback was delayed. | 69 * scheduled for - even if the actual callback was delayed. |
71 */ | 70 */ |
72 factory Timer.periodic(Duration duration, | 71 factory Timer.periodic(Duration duration, void callback(Timer timer)) { |
73 void callback(Timer timer)) { | |
74 if (Zone.current == Zone.ROOT) { | 72 if (Zone.current == Zone.ROOT) { |
75 // No need to bind the callback. We know that the root's timer will | 73 // No need to bind the callback. We know that the root's timer will |
76 // be invoked in the root zone. | 74 // be invoked in the root zone. |
77 return Zone.current.createPeriodicTimer(duration, callback); | 75 return Zone.current.createPeriodicTimer(duration, callback); |
78 } | 76 } |
79 // TODO(floitsch): the return type should be 'void', and the type | 77 // TODO(floitsch): the return type should be 'void', and the type |
80 // should be inferred. | 78 // should be inferred. |
81 var boundCallback = Zone.current.bindUnaryCallback<dynamic, Timer>( | 79 var boundCallback = Zone.current |
82 callback, runGuarded: true); | 80 .bindUnaryCallback<dynamic, Timer>(callback, runGuarded: true); |
83 return Zone.current.createPeriodicTimer(duration, boundCallback); | 81 return Zone.current.createPeriodicTimer(duration, boundCallback); |
84 } | 82 } |
85 | 83 |
86 /** | 84 /** |
87 * Runs the given [callback] asynchronously as soon as possible. | 85 * Runs the given [callback] asynchronously as soon as possible. |
88 * | 86 * |
89 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. | 87 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. |
90 */ | 88 */ |
91 static void run(void callback()) { | 89 static void run(void callback()) { |
92 new Timer(Duration.ZERO, callback); | 90 new Timer(Duration.ZERO, callback); |
93 } | 91 } |
94 | 92 |
95 /** | 93 /** |
96 * Cancels the timer. | 94 * Cancels the timer. |
97 */ | 95 */ |
98 void cancel(); | 96 void cancel(); |
99 | 97 |
100 /** | 98 /** |
101 * Returns whether the timer is still active. | 99 * Returns whether the timer is still active. |
102 * | 100 * |
103 * A non-periodic timer is active if the callback has not been executed, | 101 * A non-periodic timer is active if the callback has not been executed, |
104 * and the timer has not been canceled. | 102 * and the timer has not been canceled. |
105 * | 103 * |
106 * A periodic timer is active if it has not been canceled. | 104 * A periodic timer is active if it has not been canceled. |
107 */ | 105 */ |
108 bool get isActive; | 106 bool get isActive; |
109 | 107 |
110 external static Timer _createTimer(Duration duration, void callback()); | 108 external static Timer _createTimer(Duration duration, void callback()); |
111 external static Timer _createPeriodicTimer(Duration duration, | 109 external static Timer _createPeriodicTimer( |
112 void callback(Timer timer)); | 110 Duration duration, void callback(Timer timer)); |
113 } | 111 } |
114 | |
OLD | NEW |