0

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! :)

DisFanJen
  • 1
  • 2
  • What, literally the only difference is removing an alert? – An0nC0d3r Oct 25 '15 at 13:45
  • 1
    @Adam, yes, and that kind of behavior is representative of code that was written in such an imperative way as to depend on its whole operation being synchronous... of which `setTimeout()` isn't, by design. – Frédéric Hamidi Oct 25 '15 at 13:48
  • 1
    Yep you're correct, was just checking that was the only difference. But thanks anyway ;) – An0nC0d3r Oct 25 '15 at 13:51
  • AdamJeffers, yup. Just Removing an alert. – DisFanJen Oct 25 '15 at 13:51
  • Yes in that case, take a good read of http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – An0nC0d3r Oct 25 '15 at 13:53
  • If it doesn't shed any light come back then... – An0nC0d3r Oct 25 '15 at 13:53
  • 1
    And fixed. Thanks to Frédéric Hamidi. I wrote one function to be asynchronous while listening for the result using a synchronous function. So basically I was suffering from brain freeze and screwed up. :) – DisFanJen Oct 25 '15 at 14:53

0 Answers0