Breaking out of a switch case within a necessary setTimeout... Need some kind of solution

StackOverflow https://stackoverflow.com/questions/22676569

سؤال

Now I know that the only use for break is to get out of a loop or a switch statement...

But I'm in a bit of a bind here... I have a one page app displaying ads, weather and properties... It works rather well, save for when our connection to our weather server is down, it displays nothing but a blank screen for thirty seconds (the timeout for this case) and moves on to the next thing in the sequence...

The process needs to run a check against a CrossXHR script to ensure that there's a connection, and if so, it starts populating various icons on the weather screen from our weather server. In our multi-page version of this app there's no issue with using a setTimeout command to check in 400ms time for a change in the icon. It it hasn't changed by this time, we assume the connection has died and moves onto the next page without issue...

A one page app is harder... I can't seem to figure out the appropriate code that could tell the switch statement in 400ms time to break if we have a failure (no icon change)...

I would appreciate any help here... I have stripped out the unnecessary and irrelevant code...

You will notice the line in question:

setTimeout("if(document.getElementById('icon-first').src.right(19) == 'images/icons/na.png'){return false;}", 400); showing return false rather than break seeing that I know that the break statement won't work in there... but this is the point where I need it to check for that image change... and if so, break out of the case and thus break out the switch statement...

I can easily verify when and if the icon changes... I just need a way of busting out of the switch when it doesn't happen after 400ms after the attempted connection... Any ideas? Many thanks in advance...

function letsDoIt(seqV,aPrV,aAdsV,xV)
{
    if(pt()=="auto")
    {
        sequence = seqV;
        properties = aPrV;
        ads = aAdsV;
        position = xV;      
        //Set picture in place.
        charV = sequence[position].left(1); _L("Next thing is " + sequence[position]);
        switch(sequence[position].left(1))
        {
            case "A":
                //Show ad
                break;
            case "P":
                //Show property
                break;
            case "W":
                _L("Auto: Showing weather");
                $("#adOrProp").val("W");
                ($("#footer").css("top")!="1008px")&&$("#footer").animate({"top":"1008px"},375);//500
                $("#property-auto").css("background-image","url(images/pool.jpg)").css("background-position","top center").css("background-size","cover");
                //Important Vars
                var weatherCode = "874";
                var weatherInt = 'aploc';
                var length = 30e3;          // length of weather (s) 
                var timeSet = length;
                var feedURL = 'http://specific-domain.com/rss/wx.php?u=13145[amp]lt=' + weatherInt + '[amp]lc=' + weatherCode + '[amp]obs=1[amp]fc=1[amp]warn=1';
                var cfmParser = (0==1)?'http://other-specific-domain.com/rssparser-usa.cfm':'http://other-specific-domain.com/rssparser.cfm';
                var parserString = cfmParser + '?url=' + feedURL;
                var request;
                function parseXML(val) {
            if (document.implementation && document.implementation.createDocument) {
              xmlDoc = new DOMParser().parseFromString(val, 'text/xml');
            }
            else if (window.ActiveXObject) {
              xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.loadXML(val);
            }
            else
            {
              alert("Your browser can't handle this script.");
              return null;
            }
            return xmlDoc;
          }

                function callback() {
                    if (request.readyState == 4) {
                        try {
                            if (request.status == 200) {
                                var ajaxData = request["responseText"];
                                var xmlDoc = parseXML(ajaxData);
                                try{
                                    //various formatting
                                }catch(e){alert("ERR:"+e.message);}
                                    //other various things
                                } else {
                                return false;
                            }
                        }
                        catch(e) {
                            return false;
                        }
                    }
                }
                request = new CrossXHR();
                request.target = 'test';
                request.onreadystatechange = callback;
                // At this point I want it to break out of this switch case if the specific icon below has not changed its image, i.e. if it is still na.png, get out of the case...
                setTimeout("if(document.getElementById('icon-first').src.right(19) == 'images/icons/na.png'){return false;}", 400); // no connection test
                request.open('GET', parserString);
                request.send();
                break;
        }
        position = (position+1)%sequence.length;
        p_id=auto_i=addy=null;
        setTimeout('letsDoIt(sequence,properties,ads,position);',timeSet+iAPT*2+2);
    }
}
هل كانت مفيدة؟

المحلول

There isn't enough code to run things and make sure it works, but I believe this is what you want.

Notice the use of request.abort. I also changed the strings in setTimeout to proper functions.

function letsDoIt(seqV, aPrV, aAdsV, xV) {
  if (pt() == "auto") {
    sequence = seqV;
    properties = aPrV;
    ads = aAdsV;
    position = xV;
    //Set picture in place.
    charV = sequence[position].left(1);
    _L("Next thing is " + sequence[position]);
    switch (sequence[position].left(1)) {
    case "A":
      //Show ad
      break;
    case "P":
      //Show property
      break;
    case "W":
      _L("Auto: Showing weather");
      $("#adOrProp").val("W");
      ($("#footer").css("top") != "1008px") && $("#footer").animate({
        "top": "1008px"
      }, 375); //500
      $("#property-auto").css("background-image", "url(images/pool.jpg)").css("background-position", "top center").css("background-size", "cover");
      //Important Vars
      var weatherCode = "874";
      var weatherInt = 'aploc';
      var length = 30e3; // length of weather (s) 
      var timeSet = length;
      var feedURL = 'http://specific-domain.com/rss/wx.php?u=13145[amp]lt=' + weatherInt + '[amp]lc=' + weatherCode + '[amp]obs=1[amp]fc=1[amp]warn=1';
      var cfmParser = (0 == 1) ? 'http://other-specific-domain.com/rssparser-usa.cfm' : 'http://other-specific-domain.com/rssparser.cfm';
      var parserString = cfmParser + '?url=' + feedURL;
      var request;

      function parseXML(val) {
        if (document.implementation && document.implementation.createDocument) {
          xmlDoc = new DOMParser().parseFromString(val, 'text/xml');
        } else if (window.ActiveXObject) {
          xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.loadXML(val);
        } else {
          alert("Your browser can't handle this script.");
          return null;
        }
        return xmlDoc;
      }

      function callback() {
        if (request.readyState == 4) {
          try {
            if (request.status == 200) {
              var ajaxData = request["responseText"];
              var xmlDoc = parseXML(ajaxData);
              try {
                //various formatting
              } catch (e) {
                alert("ERR:" + e.message);
              }
              //other various things
            } else {
              return false;
            }
          } catch (e) {
            return false;
          }
        }
      }
      request = new CrossXHR();
      request.target = 'test';
      request.onreadystatechange = callback;

      var timeOut = window.setTimeout(function () {
        if (document.getElementById('icon-first').src.right(19) == 'images/icons/na.png') {
          request.abort();

          // Re-call the function to get the ball rolling again?
          // Or whatever else you'd like to do.
          letsDoIt(seqV, aPrV, aAdsV, xV)
        }
      }, 400);

      request.open('GET', parserString);
      request.send();
      break;
    }
    position = (position + 1) % sequence.length;
    p_id = auto_i = addy = null;
    setTimeout(function() {
      letsDoIt(sequence,properties,ads,position);
    },  timeSet + iAPT * 2 + 2);
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top