| 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 14 matching lines...) Expand all Loading... |
| 25 * var duration = milliseconds == null ? TIMEOUT : ms * milliseconds; | 25 * var duration = milliseconds == null ? TIMEOUT : ms * milliseconds; |
| 26 * return new Timer(duration, handleTimeout); | 26 * return new Timer(duration, handleTimeout); |
| 27 * } | 27 * } |
| 28 * ... | 28 * ... |
| 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 | 39 |
| 40 /** | 40 /** |
| 41 * Creates a new timer. | 41 * Creates a new timer. |
| 42 * | 42 * |
| 43 * The [callback] function is invoked after the given [duration]. | 43 * The [callback] function is invoked after the given [duration]. |
| 44 * | 44 * |
| 45 */ | 45 */ |
| 46 factory Timer(Duration duration, void callback()) { | 46 factory Timer(Duration duration, void callback()) { |
| 47 return _Zone.current.createTimer(duration, 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)); |
| 48 } | 54 } |
| 49 | 55 |
| 50 /** | 56 /** |
| 51 * Creates a new repeating timer. | 57 * Creates a new repeating timer. |
| 52 * | 58 * |
| 53 * The [callback] is invoked repeatedly with [duration] intervals until | 59 * The [callback] is invoked repeatedly with [duration] intervals until |
| 54 * canceled with the [cancel] function. | 60 * canceled with the [cancel] function. |
| 55 */ | 61 */ |
| 56 factory Timer.periodic(Duration duration, | 62 factory Timer.periodic(Duration duration, |
| 57 void callback(Timer timer)) { | 63 void callback(Timer timer)) { |
| 58 return _Zone.current.createPeriodicTimer(duration, callback); | 64 if (Zone.current == Zone.ROOT) { |
| 65 // No need to bind the callback. We know that the root's timer will |
| 66 // be invoked in the root zone. |
| 67 return Zone.current.createPeriodicTimer(duration, callback); |
| 68 } |
| 69 return Zone.current.createPeriodicTimer( |
| 70 duration, Zone.current.bindUnaryCallback(callback, runGuarded: true)); |
| 59 } | 71 } |
| 60 | 72 |
| 61 /** | 73 /** |
| 62 * Runs the given [callback] asynchronously as soon as possible. | 74 * Runs the given [callback] asynchronously as soon as possible. |
| 63 * | 75 * |
| 64 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. | 76 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. |
| 65 */ | 77 */ |
| 66 static void run(void callback()) { | 78 static void run(void callback()) { |
| 67 new Timer(Duration.ZERO, callback); | 79 new Timer(Duration.ZERO, callback); |
| 68 } | 80 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 79 * and the timer has not been canceled. | 91 * and the timer has not been canceled. |
| 80 * | 92 * |
| 81 * A periodic timer is active if it has not been canceled. | 93 * A periodic timer is active if it has not been canceled. |
| 82 */ | 94 */ |
| 83 bool get isActive; | 95 bool get isActive; |
| 84 } | 96 } |
| 85 | 97 |
| 86 external Timer _createTimer(Duration duration, void callback()); | 98 external Timer _createTimer(Duration duration, void callback()); |
| 87 external Timer _createPeriodicTimer(Duration duration, | 99 external Timer _createPeriodicTimer(Duration duration, |
| 88 void callback(Timer timer)); | 100 void callback(Timer timer)); |
| OLD | NEW |