Skip to content

Commit

Permalink
Make a new AbortController on every fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
shawyu-okta committed Oct 30, 2024
1 parent 21aadaa commit 742603e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const originalTime = new Date().getTime();
let lastTime = originalTime;
const abortController = new AbortController();
const abortSignal = abortController.signal;
let abortController;
let abortSignal;
let timeout;

const main = (useExperiment = false) => {
Expand All @@ -11,7 +11,9 @@ const main = (useExperiment = false) => {
if (timeout) {
clearTimeout(timeout);
}
abortController.abort("Document was hidden, aborting request");
if (abortController) {
abortController.abort("Document was hidden, aborting request");
}
} else {
console.warn("Document is visible again, re-attempting request");
timeout = setTimeout(makeRequest, 2000);
Expand All @@ -27,14 +29,18 @@ const main = (useExperiment = false) => {
method: "post",
}
if (useExperiment) {
abortController = new AbortController();
abortSignal = abortController.signal;
options.signal = abortSignal;
}
const response = await fetch(url, options);
abortSignal = null;
abortController = null;
const time = new Date().getTime();

if (response.ok) {
requestCount++;
const responseJson = await response.json();
// const responseJson = await response.json();
const outputLine = `Response success [${response.status}] n=${requestCount}, time=${time}, delta=${time - lastTime}`;
console.warn(outputLine);
const newLine = document.createElement("div");
Expand Down

0 comments on commit 742603e

Please sign in to comment.