admin管理员组文章数量:1323200
I have an Ajax call that I would like to retry when failing, based on this current code :
$.ajax({
url: "<%= my_ruby_on_rails_controller_url_here %>",
datatype: "json",
type: "GET",
success: function (json) {
document.getElementById("factureload" + json.hashed_id).innerHTML = "<a href='" + json.address + "'><img class='pdficonstyling' src='/assets/pdf4.svg' alt='pdf icon' /> facture_" + json.numfacture + "</a>";
},
error: function () {
alert("fail");
}
});
I have tried to encapsulate it inside a new function with a callback to this function in error
(together with setTimeout
) but it never starts ...
Also There could be concurrent Ajax calls such as this one for different dom elements.
(there is this useful thread but I am so bad in JS I cannot adapt it to my code How to repeat ajax call until success )
I have an Ajax call that I would like to retry when failing, based on this current code :
$.ajax({
url: "<%= my_ruby_on_rails_controller_url_here %>",
datatype: "json",
type: "GET",
success: function (json) {
document.getElementById("factureload" + json.hashed_id).innerHTML = "<a href='" + json.address + "'><img class='pdficonstyling' src='/assets/pdf4.svg' alt='pdf icon' /> facture_" + json.numfacture + "</a>";
},
error: function () {
alert("fail");
}
});
I have tried to encapsulate it inside a new function with a callback to this function in error
(together with setTimeout
) but it never starts ...
Also There could be concurrent Ajax calls such as this one for different dom elements.
(there is this useful thread but I am so bad in JS I cannot adapt it to my code How to repeat ajax call until success )
Share Improve this question asked Dec 12, 2017 at 23:28 MaxenceMaxence 2,3394 gold badges24 silver badges44 bronze badges2 Answers
Reset to default 8That link you posted contains the exact answer ... you just need to wrap your code in a function so it can be used recursively:
function myAjaxRequest () {
$.ajax({
url: "<%= my_ruby_on_rails_controller_url_here %>",
datatype: "json",
type: "GET",
success: function (json) {
document.getElementById("factureload" + json.hashed_id).innerHTML = "<a href='" + json.address + "'><img class='pdficonstyling' src='/assets/pdf4.svg' alt='pdf icon' /> facture_" + json.numfacture + "</a>";
},
error: function () {
setTimeout(() => {
myAjaxRequest()
}, 5000) // if there was an error, wait 5 seconds and re-run the function
}
})
}
myAjaxRequest()
function tryUntilSuccess(success) {
$.ajax({
url: '<%= my_ruby_on_rails_controller_url_here %>',
datatype: 'json',
type: 'GET',
success: success,
error: function(err) {
console.log('Request failed. Retrying...', err)
tryUntilSuccess(success)
},
})
}
tryUntilSuccess(function onSuccess(json) {
document.getElementById("factureload" + json.hashed_id).innerHTML = "<a href='" + json.address + "'><img class='pdficonstyling' src='/assets/pdf4.svg' alt='pdf icon' /> facture_" + json.numfacture + "</a>";
})
本文标签: javascriptRetry ajax call until successfulStack Overflow
版权声明:本文标题:javascript - Retry ajax call until successful - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742088566a2420127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论