现在我知道break
的唯一用途是退出循环或switch
声明......
但我在这里有点绑定...我有一个单页应用程序显示广告,天气和属性...它工作得很好,除了我们与我们的天气服务器的连接关闭时,它除了空白屏幕三十秒钟(这种情况下的超时),然后继续显示序列中的下一个... ...
该过程需要针对CrossXHR脚本运行检查以确保存在连接,如果是,则开始从我们的气象服务器在天气屏幕上填充各种图标。在我们这个应用程序的多页面版本中,使用setTimeout
命令检查400毫秒的图标更改时间没有问题。到目前为止它没有改变,我们假设连接已经死亡并且没有问题就移到下一页......
单页应用程序更难......我似乎无法找出适当的代码,如果我们发生故障,可以在400毫秒的时间内将switch
语句告诉break
(没有图标变化)...
我很感激这里的任何帮助......我已经删除了不必要的和不相关的代码......
你会注意到有问题的一行:
setTimeout("if(document.getElementById('icon-first').src.right(19) == 'images/icons/na.png'){return false;}", 400);
显示return false
而不是break
,因为我知道break
声明在那里不起作用......但这是我需要的地方它来检查图像更改...如果是这样,请突破case
,从而打破switch
声明......
我可以轻松验证图标何时以及是否发生变化...我只是需要一种方法来突破switch
,当它在尝试连接后400毫秒后没有发生...任何想法?非常感谢...
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);
}
}
答案 0 :(得分:0)
没有足够的代码来运行并确保它有效,但我相信这就是你想要的。
请注意request.abort
的使用。我还将setTimeout
中的字符串更改为正确的函数。
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);
}
}