I'm trying to figure out how to add a setTimeout
to my request response when using await fetch. Currently, each response is being sent to a DIV in the HTML, but I want to introduce a delay of 5 seconds before each response appears.
I attempted this solution, but unfortunately, it didn't work for me.
const envSoli = async () => {
try {
const controller = new AbortController();
const signal = controller.signal;
const timeId = setTimeout(() => {
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);
const oreen = await peticion.json();
const takeAMoment = setTimeout(() => {
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;
}, 5 * 1000);
clearTimeout(takeAMoment);
} 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();
UPDATED WORKING CODE:
const envSoli = async () => {
try {
const controller = new AbortController();
const signal = controller.signal;
const timeId = setTimeout(() => {
controller.abort();
}, 20 * 1000);
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();