Re-running a script once a minute

I have a simple script to read and echo a text file and close the file. The file is constantly being edited, so I want to re-run the script once a minute. I know I did this some time ago but I’m 84 and don’t remember how I did it. LOL

Found the answer
header(“refresh: 30”);

Here’s a simple example:

<?php
// Set the page to refresh every 60 seconds
header("refresh: 60");

// lets say your file is named "data.txt"
$filename = "data.txt";

// Check if the file exists
if (file_exists($filename)) {
    // Read and display the file content
    $content = file_get_contents($filename);
    echo nl2br($content); // nl2br() will convert new lines in the text file to <br> in HTML
} else {
    echo "File not found!";
}
?>

or you can use HTML

<meta http-equiv="refresh" content="60">

Refreshing the page once a minute is not a good idea. Use Firebase real-time database instead. It automatically changes on the page when the data changes.

Sorry, but in this case that would be using a sledge hammer to drive a nail. I’m not talking about a data base but just a text file of less than thirty lines.

Some people love their Power tools, when a simple tool is all that is needed…

Use whatever you deem best for your project…

With a bit of javascript you can make a timed file-loader:

<!DOCTYPE html>
<html>
	<head>
		<title>File (re)Loader</title>
	</head>
	<body>
		<h1>File (re)Loader</h1>
		<h3>Content of [data.txt]</h3>
		<p>Reloading in <span id="load-counter">60</span> seconds</p>
		<pre id="file-content"></pre>
		<script>
var counter = 59;		
const loadFile = () => 
{
	counter++;
	if (counter===60)
	{
		counter=0;
// Note:
// Quick And Dirty direct call to data.txt.
// Use a server side (php) script call so serve your data.txt content		
		fetch('data.txt', {cache:"no-store"})
		.then(response => response.text())
		.then((data) => 
		{
			document.getElementById('file-content').innerText = data;
		});	
    }
	document.getElementById('load-counter').innerText = (60-counter);
}

const startLoader = () =>
{
	loadFile();
    setInterval(loadFile, 1000);
}

(function (fn) 
{
    if (document.readyState === "complete" || document.readyState === "interactive") 
    {
        setTimeout(fn, 1);
    } 
    else
    {
        document.addEventListener("DOMContentLoaded", fn);
    }
}(startLoader));    
		</script>
	</body>
</html>
Sponsor our Newsletter | Privacy Policy | Terms of Service