123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- goog.provide('goog.async.ConditionalDelay');
- goog.require('goog.Disposable');
- goog.require('goog.async.Delay');
- goog.async.ConditionalDelay = function(listener, opt_handler) {
- goog.async.ConditionalDelay.base(this, 'constructor');
-
- this.interval_ = 0;
-
- this.runUntil_ = 0;
-
- this.isDone_ = false;
-
- this.listener_ = listener;
-
- this.handler_ = opt_handler;
-
- this.delay_ = new goog.async.Delay(
- goog.bind(this.onTick_, this), 0 , this );
- };
- goog.inherits(goog.async.ConditionalDelay, goog.Disposable);
- goog.async.ConditionalDelay.prototype.disposeInternal = function() {
- this.delay_.dispose();
- delete this.listener_;
- delete this.handler_;
- goog.async.ConditionalDelay.superClass_.disposeInternal.call(this);
- };
- goog.async.ConditionalDelay.prototype.start = function(
- opt_interval, opt_timeout) {
- this.stop();
- this.isDone_ = false;
- var timeout = opt_timeout || 0;
- this.interval_ = Math.max(opt_interval || 0, 0);
- this.runUntil_ = timeout < 0 ? -1 : (goog.now() + timeout);
- this.delay_.start(
- timeout < 0 ? this.interval_ : Math.min(this.interval_, timeout));
- };
- goog.async.ConditionalDelay.prototype.stop = function() {
- this.delay_.stop();
- };
- goog.async.ConditionalDelay.prototype.isActive = function() {
- return this.delay_.isActive();
- };
- goog.async.ConditionalDelay.prototype.isDone = function() {
- return this.isDone_;
- };
- goog.async.ConditionalDelay.prototype.onSuccess = function() {
-
- };
- goog.async.ConditionalDelay.prototype.onFailure = function() {
-
- };
- goog.async.ConditionalDelay.prototype.onTick_ = function() {
- var successful = this.listener_.call(this.handler_);
- if (successful) {
- this.isDone_ = true;
- this.onSuccess();
- } else {
-
- if (this.runUntil_ < 0) {
-
- this.delay_.start(this.interval_);
- } else {
- var timeLeft = this.runUntil_ - goog.now();
- if (timeLeft <= 0) {
- this.onFailure();
- } else {
- this.delay_.start(Math.min(this.interval_, timeLeft));
- }
- }
- }
- };
|