Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- const origSetInterval = window.setInterval;
- window.setInterval = function (fc, period, runImmediately=false) {
- if (runImmediately) fc();
- return origSetInterval(fc, period);
- };
- })();
- const ERR_ETH_MAX = 10;
- function PromiseETH(url_ask, sendData = '', timeout = 950, beforeSend = function () { }) {
- //return new Promise((resolve, reject) => setTimeout(() => resolve('1;2;3;'), 10));
- return new Promise((resolve, reject) =>
- $.ajax({
- url: URL_BASE + url_ask + sendData, timeout,
- beforeSend,
- success: function (data) {
- // Promise returns the received data
- errETH = 0;
- resolve(data);
- },
- error: function (request, status, error) {
- // Count ERR_ETH_MAX consecutive errors
- if (++errETH == ERR_ETH_MAX) {
- errETH = 0;
- // Check user's wish
- if (!confirm(`Ocorreram ${ERR_ETH_MAX} erros de comunicação seguidos!
- Continuar tentando?`)) {
- reject(); return;// Stop trying
- }
- }
- // Try again
- PromiseETH(url_ask, sendData)
- .then((data) => resolve(data))
- .catch(() => reject())
- }
- })
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement