Tuesday, September 5, 2023

Javascript to Check URL Every Second

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);

 
This code sends a GET request to the /getresult.html endpoint on the server and retrieves the response as JSON data. It then checks if the "translatedtext" key in the received JSON data has a non-empty value. If it does, it updates the text of a <div> element with id="data" with the new data retrieved from the server. If it doesn't, it stops sending requests by clearing the interval.
Use the setInterval function to repeatedly invoke the fetchData function at a specified interval. For example:
 
const intervalId = setInterval(fetchData, 1000);
 
This code invokes the fetchData function every 1000 milliseconds (i.e., every second) to retrieve the latest data from the server and update the page content. It also stores the interval ID in a variable so that it can be cleared later. Note that the specific implementation of the Fetch request and the DOM manipulation will depend on your application's requirements.
 
function getresultEtransJSON(requestId) {
    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!'
                    }
                }
            });
    }
}
 
 
function getresultEtrans(requestId) {
    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!'
                    }
                }
            });
    }
}