t-comm
Version:
专业、稳定、纯粹的工具库
60 lines (57 loc) • 1.68 kB
JavaScript
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/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(this, PollingRequest);
this.maxRequest = 0;
this.maxPollingTime = maxPollingTime;
this.timeInterval = timeInterval;
this.timer = null;
}
/**
* 重置,即取消轮询
*/
return _createClass(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 生成文档
export { PollingRequest };