function fetchData() {
fetch('/getresult.html')
.then(response => response.json())
.then(data => {
// Update the content of the page with the new data
document.getElementById('data').innerText = data;
});
}
setInterval(fetchData, 1000);
To stop sending requests after the received JSON data has a non-empty value in the key "translatedtext", you can modify the JavaScript code that fetches the data from the server to include a check for the "translatedtext" key in the received JSON data.
In the show.html page, use JavaScript to define a function that makes a Fetch request to the /getresult.html endpoint and updates the page content with the retrieved data.
function fetchData() {
fetch('/getresult.html')
.then(response => response.json())
.then(data => {
// Check if the "translatedtext" key has a non-empty value
if (data.translatedtext) {
// Update the content of the page with the new data
document.getElementById('data').innerText = data.translatedtext;
} else {
// Stop sending requests
clearInterval(intervalId);
}
});
}
const intervalId = setInterval(fetchData, 1000);
Use the setInterval function to repeatedly invoke the fetchData function at a specified interval. For example:
console.log(requestId);
url = '/checkResult/' + String(requestId)
let runtime = 0;
const intervalId = setInterval(fetchData, 200);
function fetchData() {
fetch(url)
.then(response => response.json())
.then(data => {
console.log(data, data[requestId] && data[requestId] == "Waiting..." && runtime <= 10);
if (data && data[requestId] == "Waiting..." && runtime <= 10) {
runtime += 0.3;
// document.getElementById('translatedtext').textContent = `${data} ${runtime} seconds`;
} else { // Stop sending requests
clearInterval(intervalId);
// <p id="translatedtext">Runtime:</p>
// document.getElementById('translatedtext').textContent = "Runtime: " + runtime + ' seconds';
if (data[requestId] != "Waiting...") {
document.getElementById('MTresultE-Translate').childNodes[0].textContent = data[requestId];
} else {
document.getElementById('MTresultE-Translate').childNodes[0].textContent = 'Connection error! Timeout after 10 seconds!'
}
}
});
}
}
console.log(requestId);
url = '/checkResult/' + String(requestId)
let runtime = 0;
const intervalId = setInterval(fetchData, 200);
function fetchData() {
fetch(url)
.then(response => response.text())
.then(data => {
console.log(data, data && data == "Waiting..." && runtime <= 10);
if (data && data == "Waiting..." && runtime <= 10) {
runtime += 0.3;
// document.getElementById('translatedtext').textContent = `${data} ${runtime} seconds`;
} else { // Stop sending requests
clearInterval(intervalId);
// <p id="translatedtext">Runtime:</p>
// document.getElementById('translatedtext').textContent = "Runtime: " + runtime + ' seconds';
if (data != "Waiting...") {
document.getElementById('MTresultE-Translate').childNodes[0].textContent = data;
} else {
document.getElementById('MTresultE-Translate').childNodes[0].textContent = 'Connection error! Timeout after 10 seconds!'
}
}
});
}
}