Skip to content

Commit 40dbc33

Browse files
committed
fetch with timeout
1 parent b845357 commit 40dbc33

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

LongevityWorldCup.Website/wwwroot/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -556,13 +556,13 @@ <h2><i class="fas fa-calendar-alt"></i> Once/Year Newsletter</h2>
556556

557557
if (validator.isEmail(emailInput)) {
558558
// Send POST request to the API
559-
fetch('/api/home/subscribe', {
559+
fetchWithTimeout('/api/home/subscribe', {
560560
method: 'POST',
561561
headers: {
562562
'Content-Type': 'application/json'
563563
},
564564
body: JSON.stringify({ email: emailInput })
565-
})
565+
}, 10000) // 10-second timeout
566566
.then(response => {
567567
if (response.ok) {
568568
return response.text(); // Or response.json() if your API returns JSON

LongevityWorldCup.Website/wwwroot/onboarding/convergence.html

+2-7
Original file line numberDiff line numberDiff line change
@@ -890,18 +890,15 @@ <h2 data-aos="fade" data-aos-duration="700" data-aos-delay="400">1. Character Cr
890890
applyButton.textContent = 'Submitting...';
891891

892892
// Send the data via fetch POST request
893-
fetch('/api/application/apply', {
893+
fetchWithTimeout('/api/application/apply', {
894894
method: 'POST',
895895
headers: {
896896
'Content-Type': 'application/json'
897897
},
898898
body: JSON.stringify(applicantData)
899-
})
899+
}, 10000) // timeout set to 10,000 ms (10 seconds)
900900
.then(response => {
901-
// Rest of your existing code...
902-
903901
if (response.ok) {
904-
// Handle success, perhaps redirect or show a success message
905902
customAlert('Application submitted successfully!').then(() => {
906903
localStorage.setItem('contactEmail', applicantData.accountEmail);
907904
// Clear the image variables
@@ -911,13 +908,11 @@ <h2 data-aos="fade" data-aos-duration="700" data-aos-delay="400">1. Character Cr
911908
window.location.href = '/onboarding/application-review.html#appReviewTitle';
912909
});
913910
} else {
914-
// Handle error
915911
customAlert('Failed to submit application. Please try again later.').then(() => {
916912
applyButton.disabled = false;
917913
applyButton.textContent = 'Apply';
918914
});
919915
}
920-
921916
})
922917
.catch(error => {
923918
console.error('Error submitting application:', error);

LongevityWorldCup.Website/wwwroot/partials/header.html

+18
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,22 @@ <h1 data-aos="fade" data-aos-duration="700" data-aos-delay="150">2025</h1>
433433
document.getElementById('custom-alert-close').addEventListener('click', function () {
434434
document.getElementById('custom-alert').close();
435435
});
436+
437+
function fetchWithTimeout(url, options = {}, timeout = 10000) {
438+
return new Promise((resolve, reject) => {
439+
const timer = setTimeout(() => {
440+
reject(new Error('Request timed out'));
441+
}, timeout);
442+
443+
fetch(url, options)
444+
.then(response => {
445+
clearTimeout(timer);
446+
resolve(response);
447+
})
448+
.catch(err => {
449+
clearTimeout(timer);
450+
reject(err);
451+
});
452+
});
453+
}
436454
</script>

LongevityWorldCup.Website/wwwroot/partials/leaderboard-content.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ <h3>Proofs</h3>
23582358
}
23592359

23602360
function fetchFullAthleteData(athleteNameText, athleteData) {
2361-
fetch('/api/data/athletes')
2361+
fetchWithTimeout('/api/data/athletes', {}, 10000) // 10-second timeout
23622362
.then(response => response.json())
23632363
.then(athletes => {
23642364
const fullAthleteData = athletes.find(a => a.Name === athleteNameText);

0 commit comments

Comments
 (0)