interval.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 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; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. import { requestAnimationFrame, cancelAnimationFrame } from './../helpers/feature';
  4. /**
  5. * @class Interval
  6. * @util
  7. */
  8. var Interval = function () {
  9. _createClass(Interval, null, [{
  10. key: 'create',
  11. value: function create(func, delay) {
  12. return new Interval(func, delay);
  13. }
  14. }]);
  15. function Interval(func, delay) {
  16. var _this = this;
  17. _classCallCheck(this, Interval);
  18. /**
  19. * Animation frame request id.
  20. *
  21. * @type {Number}
  22. */
  23. this.timer = null;
  24. /**
  25. * Function to invoke repeatedly.
  26. *
  27. * @type {Function}
  28. */
  29. this.func = func;
  30. /**
  31. * Number of milliseconds that function should wait before next call.
  32. */
  33. this.delay = parseDelay(delay);
  34. /**
  35. * Flag which indicates if interval object was stopped.
  36. *
  37. * @type {Boolean}
  38. * @default true
  39. */
  40. this.stopped = true;
  41. /**
  42. * Interval time (in milliseconds) of the last callback call.
  43. *
  44. * @private
  45. * @type {Number}
  46. */
  47. this._then = null;
  48. /**
  49. * Bounded function `func`.
  50. *
  51. * @private
  52. * @type {Function}
  53. */
  54. this._callback = function () {
  55. return _this.__callback();
  56. };
  57. }
  58. /**
  59. * Start loop.
  60. *
  61. * @returns {Interval}
  62. */
  63. _createClass(Interval, [{
  64. key: 'start',
  65. value: function start() {
  66. if (this.stopped) {
  67. this._then = Date.now();
  68. this.stopped = false;
  69. this.timer = requestAnimationFrame(this._callback);
  70. }
  71. return this;
  72. }
  73. /**
  74. * Stop looping.
  75. *
  76. * @returns {Interval}
  77. */
  78. }, {
  79. key: 'stop',
  80. value: function stop() {
  81. if (!this.stopped) {
  82. this.stopped = true;
  83. cancelAnimationFrame(this.timer);
  84. this.timer = null;
  85. }
  86. return this;
  87. }
  88. /**
  89. * Loop callback, fired on every animation frame.
  90. *
  91. * @private
  92. */
  93. }, {
  94. key: '__callback',
  95. value: function __callback() {
  96. this.timer = requestAnimationFrame(this._callback);
  97. if (this.delay) {
  98. var now = Date.now();
  99. var elapsed = now - this._then;
  100. if (elapsed > this.delay) {
  101. this._then = now - elapsed % this.delay;
  102. this.func();
  103. }
  104. } else {
  105. this.func();
  106. }
  107. }
  108. }]);
  109. return Interval;
  110. }();
  111. export default Interval;
  112. export function parseDelay(delay) {
  113. if (typeof delay === 'string' && /fps$/.test(delay)) {
  114. delay = 1000 / parseInt(delay.replace('fps', '') || 0, 10);
  115. }
  116. return delay;
  117. }