<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1.0">
<title>Page Stability Tests</title>
<script>
onload = () => {
const fetch_path = '/fetchtarget.html';
const fetch_button = document.getElementById('btnFetch');
const fetch_and_work_button = document.getElementById('btnFetchAndWork');
const work_forever_button = document.getElementById('btnWorkForever');
const paint_button = document.getElementById('btnPaint');
const output = document.getElementById('output');
window.doFetch = function(afterFetch) {
window.fetch(fetch_path)
.then((res) => {
if (!res.ok) {
output.innerText = `FAILED: ${res.status}`;
} else {
res.text().then((str) => {
output.innerText = str;
afterFetch();
});
}
})
.catch(error => { output.innerText = error; });
}
window.doBusyWork = function(tasks_to_run = 3, task_duration_ms = 300) {
const task_delay_ms = 0;
let task = (num_tasks) => {
start_time = performance.now();
while(performance.now() - start_time < task_duration_ms) {
}
num_tasks--;
if (num_tasks > 0) {
setTimeout(() => task(num_tasks), task_delay_ms)
} else {
output.innerText = "WORK DONE";
}
}
setTimeout(() => task(tasks_to_run), task_delay_ms);
}
fetch_button.onclick = () => doFetch(() => {});
fetch_and_work_button.onclick = () => doFetch(doBusyWork);
work_forever_button.onclick = () => {
doBusyWork(10000);
};
let paint_count = 0;
paint_button.onclick = () => {
paint_count++;
output.innerText = 'PAINT ' + paint_count;
};
window.navigation.addEventListener('navigate', (e) => {
if (!e.hashChange) {
return;
}
const hash = e.destination.url.split('#').slice(-1);
if (hash.length == 0) {
console.log("Unexpected URL: ", e.destination.url);
return;
}
if (hash[0] === 'fetch') {
doFetch(() => {})
} else {
console.log("Unexpected fragment: ", hash[0]);
}
});
}
</script>
</head>
<body>
<button id="btnFetch">Fetch</button>
<button id="btnFetchAndWork">Fetch and Work</button>
<button id="btnWorkForever">Work Forever</button>
<button id="btnPaint">Paint</button>
<p id="output">INITIAL</p>
</body>
</html>