所以,这可能有点令人困惑。不久前,当Chromium决定取消他们的模式支持时,我们所在的地方发现了一个看起来很好用的库(是的,他们希望在webkit/非webkit之间解析它[基本上我们有两个版本的这个东西在Firefox或Chrome中运行])
function AddToConfig(){
....
....
....
if(/ipad|android|webkit/i.test(navigator.userAgent)){
$.showModalDialog({
url: "get_required.php?maxSelect="+max+"&forceReload="+Math.random(),
dialogArguments: window,
height: 270,
width: 510,
position: 'top',
onClose: function(){ numReq = this.returnValue; return retAction(numReq); }
});
}else{
numReq = window.showModalDialog ("get_required.php?maxSelect="+max+"&forceReload="+Math.random(), window, "dialogHeight:250px; dialogWidth:500px; center:yes; help:no; resizable:no; scroll:no; status:no; edge:raised");
return numReq;
}
(点击此处获取更多信息)http://extremedev.blogspot.com/2011/03/windowshowmodaldialog-cross-browser-new.html
有一段时间,它运行良好。到目前为止,所有的代码替换都是这样的情况:用户点击按钮,弹出(现在是模态的),它做自己的事情,并返回一个值,99%的时间需要刷新iFrame或其他东西,所以时间不是问题。太棒了,太棒了。
我现在的情况是,这个模态调用正在进行一系列其他计算。如果它沿着这条开斋鸡链往下走,就会着火。然而,上面的代码似乎使它成为一个问题。我试过做下面这样的回调(也许我只是写错了),但没有奏效。见鬼,上面URL中的评论说这样做有点棘手。这里有点(我希望)就足够了。因此,我想点击AddToConfig()、ClearSelections()、printText(),这本身似乎很简单,但GetRequired异步调用会把一切都搞砸
有问题的主要代码(原件):
//What kicks it off
<input type=button class="button3" id=btnAdd onClick='AddToConfig()' value='Add' onMouseDown="this.className='button3down'" onMouseUp="this.className='button3over'" onMouseOver="this.className='button3over'" onMouseOut="this.className='button3'">
function AddToConfig(){
....
...
if (meowmeowmeowmeow)
{
....
}
else
{
//Modal - NEEDS TO PAUSE
var requiredCount = GetRequired(arrConfigSet[--lenS][2]);
//Modal - NEEDS TO PAUSE
arrConfigSet[lenS][1] = requiredCount;
arrConfigSet[lenS][2]++;
}
arrConfigItem[lenI] = new Array ();
...
...
ClearSelections();
}
function GetRequired(max)
{
//This is modal and dumb and makes me cry
var numReq = '';
if(/ipad|android|webkit/i.test(navigator.userAgent)){
$.showModalDialog({
url: "get_required.php?maxSelect="+max+"&forceReload="+Math.random(),
dialogArguments: window,
height: 270,
width: 510,
position: 'top',
onClose: function(){ numReq = this.returnValue; return numReq; }
});
}else{
//This is fine, because it pops up a new window.
numReq = window.showModalDialog ("get_required.php?maxSelect="+max+"&forceReload="+Math.random(), window, "dialogHeight:250px; dialogWidth:500px; center:yes; help:no; resizable:no; scroll:no; status:no; edge:raised");
return numReq;
}
}
function ClearSelections(){
//various UI resetting of values
printText();
}
有问题的主要代码(我的回调版本):
<input type=button class="button3" id=btnAdd onClick='AddToConfig(function(){ClearSelections();})' value='Add' onMouseDown="this.className='button3down'" onMouseUp="this.className='button3over'" onMouseOver="this.className='button3over'" onMouseOut="this.className='button3'">
if (meowmeowmeowmeow)
{
....
}
else
{
var requiredCount = GetRequired(arrConfigSet[--lenS][2]);
//So, here's my problem, that line above works fine with Firefox/non webkit because
//Popup windows make things work fine and great. With this modal thing we found
//on the internet (in getRequired()) this line of code keeps going and runs stuff below
// the line and keeps going. I need this to
//stop here. I've tried to do something like put that requiredCount in a while loop, but
//then it says Uncaught TypeError: Cannot read property '2' of undefined on that var requiredCount.
//For the record, this is all done from a onClick event on a button if it makes a difference.
//
//
//alert('test:' + arrConfigSet[--lenS][2]); //Returns a value!
// var requiredCount;
// while(typeof variable_here === 'undefined'){
// requiredCount = GetRequired(arrConfigSet[--lenS][2]);
// }
//This keeps running when in Webkit, I need that requiredCount to be set and finalized with
//the modal window gone before this continues.
arrConfigSet[lenS][1] = requiredCount;
arrConfigSet[lenS][2]++;
}
arrConfigItem[lenI] = new Array ();
...
...
callback();
}
function GetRequired(max)
{
//This is modal and dumb and makes me cry
var numReq = '';
if(/ipad|android|webkit/i.test(navigator.userAgent)){
$.showModalDialog({
url: "get_required.php?maxSelect="+max+"&forceReload="+Math.random(),
dialogArguments: window,
height: 270,
width: 510,
position: 'top',
onClose: function(){ numReq = this.returnValue; return numReq; }
});
}else{
//This is fine, because it pops up a new window.
numReq = window.showModalDialog ("get_required.php?maxSelect="+max+"&forceReload="+Math.random(), window, "dialogHeight:250px; dialogWidth:500px; center:yes; help:no; resizable:no; scroll:no; status:no; edge:raised");
return numReq;
}
}
function ClearSelections(){
//various UI resetting of values
printText();
}
有人知道如何让JS停止运行,直到脚本获得返回值吗?它做得很好,但当它做的时候,它运行后的其余代码。
请注意,我们运行的是jQuery1.4,所以promise()是不可能的,我也不想推出任何更新的东西。这个代码有点像老鼠窝。
您可以实现自己的简单延迟库,也可以简单地传入一个函数,该函数在一切完成时被调用。简单示例:
var whenDone = function(res) { alert('I'm Done: ' + res); };
doComplexTask(whenDone);
以及doComplexTask:
function doComplexTask(completeFn) {
//Do some crazy work here that takes a long time (Or is asynchronous)
var res = crazyWork();
completeFn(res);
}
在这里可以找到一个简单的jfiddle。