Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. (() => {
  2. const origSetInterval = window.setInterval;
  3. window.setInterval = function (fc, period, runImmediately=false) {
  4. if (runImmediately) fc();
  5. return origSetInterval(fc, period);
  6. };
  7. })();
  8.  
  9. const ERR_ETH_MAX = 10;
  10. function PromiseETH(url_ask, sendData = '', timeout = 950, beforeSend = function () { }) {
  11. //return new Promise((resolve, reject) => setTimeout(() => resolve('1;2;3;'), 10));
  12. return new Promise((resolve, reject) =>
  13. $.ajax({
  14. url: URL_BASE + url_ask + sendData, timeout,
  15. beforeSend,
  16. success: function (data) {
  17. // Promise returns the received data
  18. errETH = 0;
  19. resolve(data);
  20. },
  21. error: function (request, status, error) {
  22. // Count ERR_ETH_MAX consecutive errors
  23. if (++errETH == ERR_ETH_MAX) {
  24. errETH = 0;
  25. // Check user's wish
  26. if (!confirm(`Ocorreram ${ERR_ETH_MAX} erros de comunicação seguidos!
  27. Continuar tentando?`)) {
  28. reject(); return;// Stop trying
  29. }
  30. }
  31. // Try again
  32. PromiseETH(url_ask, sendData)
  33. .then((data) => resolve(data))
  34. .catch(() => reject())
  35. }
  36. })
  37. )
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement