uniapp,h5

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. /**
  3. * Initialize backoff timer with `opts`.
  4. *
  5. * - `min` initial timeout in milliseconds [100]
  6. * - `max` max timeout [10000]
  7. * - `jitter` [0]
  8. * - `factor` [2]
  9. *
  10. * @param {Object} opts
  11. * @api public
  12. */
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.Backoff = Backoff;
  15. function Backoff(opts) {
  16. opts = opts || {};
  17. this.ms = opts.min || 100;
  18. this.max = opts.max || 10000;
  19. this.factor = opts.factor || 2;
  20. this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
  21. this.attempts = 0;
  22. }
  23. /**
  24. * Return the backoff duration.
  25. *
  26. * @return {Number}
  27. * @api public
  28. */
  29. Backoff.prototype.duration = function () {
  30. var ms = this.ms * Math.pow(this.factor, this.attempts++);
  31. if (this.jitter) {
  32. var rand = Math.random();
  33. var deviation = Math.floor(rand * this.jitter * ms);
  34. ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
  35. }
  36. return Math.min(ms, this.max) | 0;
  37. };
  38. /**
  39. * Reset the number of attempts.
  40. *
  41. * @api public
  42. */
  43. Backoff.prototype.reset = function () {
  44. this.attempts = 0;
  45. };
  46. /**
  47. * Set the minimum duration
  48. *
  49. * @api public
  50. */
  51. Backoff.prototype.setMin = function (min) {
  52. this.ms = min;
  53. };
  54. /**
  55. * Set the maximum duration
  56. *
  57. * @api public
  58. */
  59. Backoff.prototype.setMax = function (max) {
  60. this.max = max;
  61. };
  62. /**
  63. * Set the jitter
  64. *
  65. * @api public
  66. */
  67. Backoff.prototype.setJitter = function (jitter) {
  68. this.jitter = jitter;
  69. };