-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (62 loc) · 2.01 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const originalTime = new Date().getTime();
let lastTime = originalTime;
let abortController;
let abortSignal;
let timeout;
const main = (useExperiment = false) => {
// Disable the start buttons
document.getElementById("start1").setAttribute("disabled", "true");
document.getElementById("start2").setAttribute("disabled", "true");
if (useExperiment) {
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
if (timeout) {
clearTimeout(timeout);
}
if (abortController) {
abortController.abort("Document was hidden, aborting request");
}
} else {
console.warn("Document is visible again, re-attempting request");
timeout = setTimeout(makeRequest, 2000);
}
});
}
let requestCount = 0;
const outputElement = document.getElementById("output");
const makeRequest = async () => {
const url = "https://httpbin.org/post";
const options = {
mode: "cors",
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 outputLine = `Response success [${response.status}] n=${requestCount}, time=${time}, delta=${time - lastTime}`;
console.warn(outputLine);
const newLine = document.createElement("div");
const newContent = document.createTextNode(outputLine);
newLine.appendChild(newContent);
lastTime = time;
timeout = setTimeout(makeRequest, 2000);
outputElement.appendChild(newLine);
} else {
lastTime = time;
timeout = setTimeout(makeRequest, 2000);
throw new Error('HTTP error! Status: ' + response.status);
}
};
timeout = setTimeout(() => {
makeRequest();
}, 2000);
};