Я не знаю в чем дело по прежнему, не работает в режиме реального времени, код setInterval я заменила на setTimeout, как советовали выше.
Вот пример:
function init()
{
Index1 = -1;
bool = true;
var timer = setTimeout(function ajaxQuery(){
var ajaxhttp = new XMLHttpRequest();
var url = "./data.json";
ajaxhttp.open("GET",url,true);
ajaxhttp.setRequestHeader("content-type","application/json");
ajaxhttp.onreadystatechange = function(){
if(ajaxhttp.readyState == 4 && ajaxhttp.status == 200){
//console.log(ajaxhttp.responseText);
jsontent = JSON.parse(ajaxhttp.responseText);
tree.innerHTML = "";
const html = recursiveList(jsontent);
tree.append(html);
output.innerHTML = "<strong>"+bool+"</strong> " + i +" == "+Index1;
posts = jsontent["response"];
if(bool)
{
switch(Index1) {
case -1: // if (x === 'value1')
posts[i].status = "current";
break;
default:
if(posts.length-1<i)
{
i = 0;
}
if(Index1 >= posts[i].photo.length)
{
posts[i].status = "ok";
i++;
Index1 = -2;
}
else
{
posts[i].photo[Index1].big.status = "ok";
}
if(posts[i]==undefined)
{
bool = false;
}
break;
}
jsontent["response"] = posts;
const jsonString = JSON.stringify(jsontent);
const xhr = new XMLHttpRequest();
xhr.open("POST",'receive.php');
xhr.setRequestHeader("Content-Type","application/json");
xhr.send(jsonString);
Index1++;
}
}
}
ajaxhttp.send();
setTimeout(ajaxQuery,1000);
},1000);
//setInterval(test,1000);
}
Также php файл я тоже изменила:
<?php
$myFile = "data.json";
$last_modified = filemtime( $myFile );
$modified_since = ( isset( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ? strtotime( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) : false );
$etagHeader = ( isset( $_SERVER["HTTP_IF_NONE_MATCH"] ) ? trim( $_SERVER["HTTP_IF_NONE_MATCH"] ) : false );
$requestPayload = file_get_contents("php://input");
$object = json_decode($requestPayload,true);
$etag = sprintf( '"%s-%s"', $last_modified, md5( $myFile ) );
header( "Last-Modified: ".gmdate( "D, d M Y H:i:s", $last_modified )." GMT" );
header( "Etag: ".$etag );
if ( (int)$modified_since === (int)$last_modified && $etag === $etagHeader ) {
header( "HTTP/1.1 304 Not Modified" );
exit;
}
file_put_contents($myFile,$requestPayload);
?>