Last active
September 1, 2021 01:41
-
-
Save axot/fad0b0a76f9747f22a49aaa59ea96cee to your computer and use it in GitHub Desktop.
poc for rendering iframe with ajax exponential backoff retry strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <iframe id='iframe' frameborder='0' height='200' width='300'></iframe> | |
var frame = document.getElementById("iframe").contentWindow.document; | |
function retryTimeout(n, maximum_backoff) { | |
maximum_backoff = (typeof maximum_backoff !== 'undefined') ? maximum_backoff : 5000; | |
if (n < 0) n = 0; | |
if (maximum_backoff < 0) maximum_backoff = 5000; | |
var time = 2**(n-1)*100 + Math.floor(Math.random()*100); | |
return Math.min(time, maximum_backoff); | |
} | |
function callAjax(count) { | |
count = (typeof count !== 'undefined') ? count : 0 | |
$.ajax({ | |
url: count == 6 ? "https://httpstat.us/200/cors" : "https://httpstat.us/502/cors", | |
retryLimit : 10, | |
success: function(result){ | |
frame.open(); | |
frame.write(result); | |
frame.close(); | |
}, | |
error : function(xhr, textStatus, errorThrown ) { | |
if (count < this.retryLimit) { | |
frame.open(); | |
frame.write(count); | |
frame.close(); | |
++count; | |
setTimeout(function() {callAjax(count);}, retryTimeout(count)); | |
return; | |
} | |
return; | |
} | |
}); | |
} | |
callAjax(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment