UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

69 lines (62 loc) 2.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _classCallCheck = require('@babel/runtime/helpers/classCallCheck'); var _createClass = require('@babel/runtime/helpers/createClass'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck); var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass); var PollingRequest = /*#__PURE__*/function () { /** * 轮询 * @constructor * @param {number} [maxPollingTime=10] 最大轮询次数 * @param {number} [timeInterval=2000] 轮询间隔 * @example * * ```ts * const polling = new PollingRequest(10); * const cb = () => { * this.onGetTeamList(true); * }; * polling.polling(cb); * ``` */ function PollingRequest() { var maxPollingTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; var timeInterval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2000; _classCallCheck__default["default"](this, PollingRequest); this.maxRequest = 0; this.maxPollingTime = maxPollingTime; this.timeInterval = timeInterval; this.timer = null; } /** * 重置,即取消轮询 */ return _createClass__default["default"](PollingRequest, [{ key: "reset", value: function reset() { this.maxRequest = 0; clearInterval(this.timer); } /** * 开始轮询 * @param {function} func 轮询方法 */ }, { key: "polling", value: function polling(func) { var _this = this; this.timer = setInterval(function () { _this.maxRequest += 1; if (_this.maxRequest > _this.maxPollingTime) { clearInterval(_this.timer); _this.maxRequest = 0; } else { func === null || func === void 0 ? void 0 : func(); } }, this.timeInterval); } }]); }(); // 这种导出可以用 jsdoc 生成文档 exports.PollingRequest = PollingRequest;