Skip to content

Instantly share code, notes, and snippets.

@gbishop
Created February 9, 2012 00:31
Show Gist options
  • Save gbishop/1775825 to your computer and use it in GitHub Desktop.
Save gbishop/1775825 to your computer and use it in GitHub Desktop.
A web-testing hack to allow me to force refresh in several browsers simultaneously.
$(function(){
/* poll for commands to run. I mostly use it with window.location.reload(true); */
function remoteCommand() {
$.ajax({
url: "/wp-content/themes/thr3/remoteCommand.php",
cache: false,
timeout: 100000,
success: function(data) {
if (data != 'no') {
eval(data);
}
setTimeout(remoteCommand, 100);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("error", textStatus + " (" + errorThrown + ")");
setTimeout(remoteCommand, 1000);
},
});
}
remoteCommand();
});
<?php
/* handle requests for commands. */
session_start();
$file = '/var/tmp/command.js';
if (!file_exists($file)) {
touch($file);
}
if (!isset($_SESSION['lasttime'])) {
$_SESSION['lasttime'] = filemtime($file);
}
$start = time();
$resp = 'no';
while (time() - $start < 30) {
if ($_SESSION['lasttime'] == filemtime($file)) {
usleep(100000);
clearstatcache();
} else {
$resp = file_get_contents($file);
$_SESSION['lasttime'] = filemtime($file);
break;
}
}
echo $resp;
?>
@gbishop
Copy link
Author

gbishop commented Feb 9, 2012

@ahokinson
Copy link

This is a really great implementation of this idea. I simply run touch command.js and I can remotely execute my javascript. Thanks for throwing this together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment