Files
shaka-player/lib/util/timer.js
T
Sandra Lokshina 6b1ca2d229 Initial release of Shaka Player UI
Other contributors:
 - @joeyparrish
 - @michellezhuogg
 - @TheModMaker
 - @theodab
 - @vaage

Change-Id: If6df33d9ab5035d1ead4402004f7de37ee8470f4
2018-11-16 14:40:37 -08:00

77 lines
1.8 KiB
JavaScript

/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.util.Timer');
/**
* A simple cancelable timer.
* @param {Function} callback
* @constructor
* @struct
* @export
*/
shaka.util.Timer = function(callback) {
/** @private {?number} */
this.id_ = null;
/** @private {Function} */
this.callback_ = (function() {
this.id_ = null;
callback();
}.bind(this));
};
/**
* Cancel the timer, if it's running.
* @export
*/
shaka.util.Timer.prototype.cancel = function() {
if (this.id_ != null) {
clearTimeout(this.id_);
this.id_ = null;
}
};
/**
* Schedule the timer, canceling any previous scheduling.
* @param {number} seconds
* @export
*/
shaka.util.Timer.prototype.schedule = function(seconds) {
this.cancel();
this.id_ = setTimeout(this.callback_, seconds * 1000);
};
/**
* Schedule the timer, canceling any previous scheduling. The timer will
* automatically reschedule after the callback fires.
* @param {number} seconds
* @export
*/
shaka.util.Timer.prototype.scheduleRepeated = function(seconds) {
this.cancel();
let repeat = (function() {
this.callback_();
this.id_ = setTimeout(repeat, seconds * 1000);
}.bind(this));
this.id_ = setTimeout(repeat, seconds * 1000);
};