t-comm
Version:
专业、稳定、纯粹的工具库
56 lines (54 loc) • 1.37 kB
JavaScript
var PollingRequest = /** @class */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(maxPollingTime, timeInterval) {
if (maxPollingTime === void 0) {
maxPollingTime = 10;
}
if (timeInterval === void 0) {
timeInterval = 2000;
}
this.maxRequest = 0;
this.maxPollingTime = maxPollingTime;
this.timeInterval = timeInterval;
this.timer = null;
}
/**
* 重置,即取消轮询
*/
PollingRequest.prototype.reset = function () {
this.maxRequest = 0;
clearInterval(this.timer);
};
/**
* 开始轮询
* @param {function} func 轮询方法
*/
PollingRequest.prototype.polling = function (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);
};
return PollingRequest;
}();
export { PollingRequest };