I want javascript to refresh the page when a modification has been made to the page- file on the server.
I use api.php (see below) to get the file modification time of the file and return it as json.
So getting data from api.php is no problem but how can I compare an old value to a new value in javascript and refresh the page if this is the case.
Here is what I have done so far:
var oldVar = 0;
setInterval(function(){
$.getJSON('api.php', function(data) {
console.log(data);
var obj = data;
var filedate = obj.filedate;
if (oldVar != filedate) {
location.reload();
};
var oldVar = filedate;
});
}, 1000); // 1 second
Obviously this makes the page refresh every second but I have no idea how to proceed.
Here is the PHP code I use for getting the data, I use the filemtime to check the file modification time on the server.
<?php
$filename = "test.php";
if (file_exists($filename)) {
$dateFile = date("s", filemtime($filename));
$dateOfFile = array('filedate' => "$dateFile");
echo json_encode($dateOfFile);
}
?>