123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- import { requestAnimationFrame, cancelAnimationFrame } from './../helpers/feature';
- /**
- * @class Interval
- * @util
- */
- var Interval = function () {
- _createClass(Interval, null, [{
- key: 'create',
- value: function create(func, delay) {
- return new Interval(func, delay);
- }
- }]);
- function Interval(func, delay) {
- var _this = this;
- _classCallCheck(this, Interval);
- /**
- * Animation frame request id.
- *
- * @type {Number}
- */
- this.timer = null;
- /**
- * Function to invoke repeatedly.
- *
- * @type {Function}
- */
- this.func = func;
- /**
- * Number of milliseconds that function should wait before next call.
- */
- this.delay = parseDelay(delay);
- /**
- * Flag which indicates if interval object was stopped.
- *
- * @type {Boolean}
- * @default true
- */
- this.stopped = true;
- /**
- * Interval time (in milliseconds) of the last callback call.
- *
- * @private
- * @type {Number}
- */
- this._then = null;
- /**
- * Bounded function `func`.
- *
- * @private
- * @type {Function}
- */
- this._callback = function () {
- return _this.__callback();
- };
- }
- /**
- * Start loop.
- *
- * @returns {Interval}
- */
- _createClass(Interval, [{
- key: 'start',
- value: function start() {
- if (this.stopped) {
- this._then = Date.now();
- this.stopped = false;
- this.timer = requestAnimationFrame(this._callback);
- }
- return this;
- }
- /**
- * Stop looping.
- *
- * @returns {Interval}
- */
- }, {
- key: 'stop',
- value: function stop() {
- if (!this.stopped) {
- this.stopped = true;
- cancelAnimationFrame(this.timer);
- this.timer = null;
- }
- return this;
- }
- /**
- * Loop callback, fired on every animation frame.
- *
- * @private
- */
- }, {
- key: '__callback',
- value: function __callback() {
- this.timer = requestAnimationFrame(this._callback);
- if (this.delay) {
- var now = Date.now();
- var elapsed = now - this._then;
- if (elapsed > this.delay) {
- this._then = now - elapsed % this.delay;
- this.func();
- }
- } else {
- this.func();
- }
- }
- }]);
- return Interval;
- }();
- export default Interval;
- export function parseDelay(delay) {
- if (typeof delay === 'string' && /fps$/.test(delay)) {
- delay = 1000 / parseInt(delay.replace('fps', '') || 0, 10);
- }
- return delay;
- }
|