Ok, I've tried searching for an answer but nothing seems to quite fit...
I've a function I've cobbled together to see if a server is running before performing an action. It does this by checking for the presence of a specific image on the remote server.
All seemed to be working as I expected until I took out the alert that was putting the filename to be checked to the screen (for debug purposes).
After removing the alert it always reported the server as down even though it was up.
Putting back an alert seems to fix it, and the alert doesn't have to be relevant (As you will see from the code).
var isPinging = false;
var serverUp;
function Pinger_ping(ip, callback) {
if(!isPinging) {
isPinging = true;
this.callback = callback
this.ip = ip;
var _that = this;
this.img = new Image();
this.img.onload = function() {
isPinging=false;
serverUp=true;
};
this.img.onerror = function() {
isPinging=false;
};
serverUp = false;
this.img.src = "http://" + ip + "/images/checkimg.jpg?random-no-cache=" + Math.floor((1 + Math.random()) * 0x10000).toString(16);
//unrem the line below and the code works as predicted.
//alert("wtf");
this.timer = setTimeout(function() {
isPinging=false;
}, 4500);
}
}
function pingServer(ipaddress = '127.0.0.1') {
var exitCode = false;
if(!isPinging)
Pinger_ping(ipaddress);
if(serverUp) {
alert("Up");
exitCode = true;
}
else
alert("Down");
return exitCode;
}
<!-- Just called by a simple onclick for testing. -->
<input type="button" value="ping" onclick="pingServer();">
Any ideas would be greatly appreciated, even if it turns out to just be my screw up! :)