@darwish/utils-core
Version:
33 lines (32 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function timerTask(callback, options) {
var _a;
var _b = options.type, type = _b === void 0 ? "action" : _b, _c = options.maxAttempts, maxAttempts = _c === void 0 ? 5 : _c, _d = options.timeout, timeout = _d === void 0 ? 1000 : _d, onSuccess = options.onSuccess, onFailure = options.onFailure;
var attempts = (_a = options.attempts) !== null && _a !== void 0 ? _a : 0;
var intervalId = setInterval(function () {
if (type === "check") {
var result = callback();
if (result) {
clearInterval(intervalId);
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
return;
}
else if (attempts >= maxAttempts) {
clearInterval(intervalId);
onFailure === null || onFailure === void 0 ? void 0 : onFailure();
return;
}
}
else {
callback();
if (attempts >= maxAttempts) {
clearInterval(intervalId);
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
return;
}
}
attempts++;
}, timeout);
}
exports.default = timerTask;