SpeedyF/static/checkjob.js

42 lines
1.2 KiB
JavaScript

console.log("jobId: ", job_id)
const fetchJobState = async () => {
try {
const response = await fetch(`/job_state?job_id=${job_id}`);
if (!response.ok) {
throw new Error('Failed to fetch job state');
}
const data = await response.json();
// Update UI with job state
document.getElementById('state-msg').innerText = data.state;
if (data.state == "error") {
document.getElementById('job_error').innerText = data.error;
document.getElementById('err_msg').style = 'display: inline-block;'
stopPolling();
}
if (data.state == "done") {
stopPolling();
showDownloadButton();
document.title = "All Done"
document.getElementById('title').textContent = "All Done"
}
} catch (error) {
console.error(error);
}
};
function stopPolling() {
clearInterval(pollingIntervalId)
}
function showDownloadButton() {
document.getElementById('download-button').style = 'display: inline-block;'
}
// Poll the endpoint every 5 seconds (adjust as needed)
const pollingInterval = 2000; //ms
const pollingIntervalId = setInterval(fetchJobState, pollingInterval);
// Initial fetch
fetchJobState();