Note that there are some explanatory texts on larger screens.

plurals
  1. POAlternative to setInterval to see if file has been modified?
    primarykey
    data
    text
    <p>I'm trying to see if a file has been modified on the server. Currently, I've got setInterval checking every second. It's using AJAX to call a PHP script that checks the file length against what the JS thinks it should be. My question is, is there a better/more efficient way to do this?</p> <p>(I'm using a mutationObserver for something else and it got me wondering if there is something else similar that could help with this...)</p> <p><strong>JS:</strong></p> <pre><code>function checkFile() { var fReader = new XMLHttpRequest(); var params = "MODE=CHECK&amp;File=Data.txt"; fReader.open("POST","File.php",false); fReader.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); fReader.setRequestHeader("Content-length", params.length); fReader.setRequestHeader("Connection", "close"); fReader.send(params); return JSON.parse(fReader.responseText).CHECK &gt; myLength; } </code></pre> <p><strong>PHP:</strong></p> <pre><code>switch($MODE) { case('CHECK'): if(file_exists($_POST['File'])){ $return['CHECK'] = count(file($_POST['File'])); } break; case('GET'): $lines = file($_POST['File']); $length=$_POST['LEN'];#This is the number of lines JS has. $count = count($lines); if ($length &lt; $count) { $return['GET'] = array_slice($lines,$length); } else { #return all lines. $return['GET'] = $lines; } break; </code></pre> <h1>...</h1> <pre><code>} echo json_encode($return); </code></pre> <p>The only other idea I had was to have AJAX check the file asynchronously and split out the response to a separate function. The JS will send over a file count (in the params) and the PHP will check that against the count. If its different, it returns the difference, otherwise it returns something like null or "no change". Currently, i'm checking the file as above. If it's changed, i AJAX again with mode GET to actually get the changes...</p> <p>Ideas? Completely different approach? I'm open to unconventional approaches. (my browser of choice is Chrome, I'm using an extension already, so i could do something in there as well...).</p> <p>Thanks, Dave</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload