在我的代码中有一个timeout
,所以如果在25秒内没有响应我的请求,请求就会被controller.abort()
取消,并显示一个Sweetalert2通知用户一个“服务器超时”。另外,我还有另外一个一般错误的甜警报。这样用户就会知道某些东西正在失败。
问题是它没有显示超时的甜蜜警告(尽管它确实取消了请求)。相反,它显示了一般的错误甜警报。
在控制台中,我得到了错误DOMException: The user aborted a request
,但无法解决它。提前谢谢。
代码:
const envSoli = async () => {
try {
const controller = new AbortController();
const signal = controller.signal;
const timeId = setTimeout(() => {
Swal.fire({
//error timeout
//custom function translate
text: translate("text1"),
icon: "info",
buttonsStyling: false,
confirmButtonText: translate("confirmbtn"),
allowOutsideClick: false,
allowEscapeKey: false,
customClass: {
confirmButton: "btn btn-info",
},
//refresh on ok click button
}).then(function () {
location.reload();
});
//controller.abort(), the idea is abort a request
//after show the sweetalert
controller.abort();
}, 20 * 1000); // 20 sec
let peticion = await fetch("data.php", {
method: "POST",
body: "ajax=1&do=check&lista=" + encodeURIComponent(leray[chenille]),
headers: { "Content-type": "application/x-www-form-urlencoded" },
cache: "no-cache",
signal: signal,
});
clearTimeout(timeId);
let oreen = await peticion.json();
switch (oreen.enviando) {
case -1:
chenille++;
document.getElementById("div1").append(oreen.cat + "<br />");
updateProgress(chenille, leray.length);
tvmit_wrongUp();
break;
case 1:
chenille++;
document.getElementById("div1").append(oreen.dog + "<br />");
updateProgress(chenille, leray.length);
tvmit_wrongUp();
break;
case 2:
chenille++;
document.getElementById("div2").append(oreen.sky + "<br />");
nieva++;
updateProgress(chenille, leray.length);
tvmit_dieUp();
break;
case 3:
chenille++;
document.getElementById("div3").append(oreen.water + "<br />");
tvmit_liveUp();
updateProgress(chenille, leray.length);
break;
}
OKTY(leray, chenille, aarsh, nieva);
return true;
} catch (error) {
console.log(error);
Swal.fire({
text: translate("text2"),
icon: "question",
buttonsStyling: false,
confirmButtonText: translate("confirmbtn"),
allowOutsideClick: false,
allowEscapeKey: false,
customClass: {
confirmButton: "btn btn-primary",
},
//refresh again on button click
}).then(function () {
location.reload();
});
}
};
envSoli();
发布于 2022-09-20 08:40:54
此错误来自controller.abort()
中止的请求,我建议您捕获错误以不停止执行。
try {
controller.abort();
} catch (e) {
console.log(e)
}
Swal.fire({
//error timeout
//custom function translate, to auto translate the text
text: translate("text1"),
icon: "info",
buttonsStyling: false,
confirmButtonText: translate("confirmbtn"),
allowOutsideClick: false,
allowEscapeKey: false,
customClass: {
confirmButton: "btn btn-info",
},
//refresh on ok click button
}).then(function () {
location.reload();
});
https://stackoverflow.com/questions/73783781
复制相似问题