I have a RPI running a web server.
There are 2 browser links that i use to modify the data in a text file, named demoRX.txt.
Dependant on the data in the demoRX.txt file the status of my webpage images are displayed.
I have javascript that gets the demoRX.txt file every 3 seconds.
It decodes the text data and sets the images to be displayed accordingly.
Initially all works well.
But if there is no change in the demoRX.txt file data for a while then it all goes wrong.
The 3 second interval timer just gets longer and longer.
See the attached apache2 log file.
There are 2 browser links that i use to modify the data in a text file, named demoRX.txt.
Dependant on the data in the demoRX.txt file the status of my webpage images are displayed.
I have javascript that gets the demoRX.txt file every 3 seconds.
It decodes the text data and sets the images to be displayed accordingly.
Initially all works well.
But if there is no change in the demoRX.txt file data for a while then it all goes wrong.
The 3 second interval timer just gets longer and longer.
See the attached apache2 log file.
Code:
setTimeout(function repeat() {
readTextFile("arduino/demoRX.txt"); // read demoRX.txt from arduino
setTimeout(repeat, 3000);
}, 3000);
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText; // read text file
setImgState(allText) // update image state
}
}
}
rawFile.send();
}
function setImgState(deviceState)
{
const deviceImgs = document.querySelectorAll("img"); // get images from dom
for (let i = 0; i < 4; i++) { // loop through image div id's
result = deviceState.substring(i, i+1); // get next device state from text string in demoRX.txt
if (result == "1") {
document.getElementById(deviceImgs[i].id).src = "images/grnLed.jpg"; // show green led if device state is ON
} else {
document.getElementById(deviceImgs[i].id).src = "images/redLed.jpg"; // show red led if device state is OFF
}
}
}