Skip to content

Instantly share code, notes, and snippets.

@dbergey
Created April 13, 2010 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbergey/365104 to your computer and use it in GitHub Desktop.
Save dbergey/365104 to your computer and use it in GitHub Desktop.
// checks every few secs to see if any source file is updated and reloads if needed
// reloadr.php should check mtimes of files you want to track and return max(mtimes).
// like this:
/*
<?php
$files = array_merge(
glob('*.php'),
glob('tests/*.php')
);
foreach ( $files as &$file ) $file = filemtime($file);
echo max($files);
?>
*/
var reloadr = {
frequency: 2000,
fname: 'reloadr.php',
req: new XMLHttpRequest(),
ajax: function(url, callback) {
reloadr.req.open("GET", url, false);
reloadr.req.send(null);
if (reloadr.req.status == 200) callback.call(reloadr.req);
},
go: function() {
window.loadTime = Date.parse(Date()) / 1000;
setInterval(function() {
reloadr.ajax(reloadr.fname, function() {
if (this.responseText > window.loadTime) location.reload();
});
}, reloadr.frequency);
}
};
reloadr.go();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment