I have a Raspberry Pi with some buttons connected to it that change a value contained in some file, say "setting.txt". For example, its contents might be as simple as
42
and that's it. The buttons trigger a process that changes the content of this file (e.g. 42 becomes 43).
The value needs to be displayed on a website run by Apache2, so I built the following:
<html>
<body>
<div id="fileContent"></div>
</body>
<script type="text/javascript">
window.onload = function() {
var $interval = 100;
setInterval(updateDivByPHP, $interval, "fileContent", "getcontent.php");
}
function updateDivByPHP($id, $phpFile) {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
document.getElementById($id).innerHTML = xmlhttp.responseText;
}
xmlhttp.open("POST", $phpFile);
xmlhttp.send();
}
</script>
</html>
where getcontent.php is as simple as
<?php
include "setting.txt";
?>
Having set the refresh interval to 100ms, I expected the refresh delay after a file-change to be resonably short, but it takes up to 2 whole seconds for the contents to be updated.
Is there a fix or more efficient/cannonical approach to what I want?
EDIT
It would be even better if the file could be skipped altogether and pipes its output directly to the website, somehow...