使用模式库从嵌套的异步调用生成javascript同步代码?

使用模式库从嵌套的异步调用生成javascript同步代码?,javascript,jquery,asynchronous,synchronous,Javascript,Jquery,Asynchronous,Synchronous,所以,这可能有点混乱。不久前,Chromium决定终止其模式支持时,我们所在的地方发现了一个似乎工作得很好的库(是的,他们希望它在webkit/非webkit之间解析出来[基本上,我们有两个在Firefox或Chrome中运行的版本]) (请点击此处查看更多信息) 有一段时间,它运作良好。到目前为止,所有的代码替换都是在用户单击按钮、弹出(现在是模态)的情况下进行的,它执行它的操作并返回一个值,99%的时间需要刷新iFrame或其他东西,所以时间不是问题。太棒了,太棒了 现在我的情况,这个模态

所以,这可能有点混乱。不久前,Chromium决定终止其模式支持时,我们所在的地方发现了一个似乎工作得很好的库(是的,他们希望它在webkit/非webkit之间解析出来[基本上,我们有两个在Firefox或Chrome中运行的版本])

(请点击此处查看更多信息)

有一段时间,它运作良好。到目前为止,所有的代码替换都是在用户单击按钮、弹出(现在是模态)的情况下进行的,它执行它的操作并返回一个值,99%的时间需要刷新iFrame或其他东西,所以时间不是问题。太棒了,太棒了

现在我的情况,这个模态调用在一堆其他计算的中间。如果它沿着这条伊夫亨斯链往下走,它就会开火。然而,上面的代码似乎使它成为一个问题。我曾经尝试过像下面这样的回调(也许我只是写错了),但没有成功。见鬼,上面URL中的评论说这样做有点棘手。这里有一些(我希望的)就足够了。 因此,我想点击AddToConfig()、ClearElections()、printText(),这本身似乎很简单,但GetRequired的asynch-y调用却把一切都抛在脑后了

有关主要代码(原件):

//是什么开始的
函数AddToConfig(){
....
...
如果(喵喵喵喵)
{
....
}
其他的
{
//模态-需要暂停
var requiredCount=GetRequired(arrConfigSet[--lenS][2]);
//模态-需要暂停
arrConfigSet[lenS][1]=所需计数;
arrConfigSet[lenS][2]++;
}
arrConfigItem[lenI]=新数组();
...
...
选举();
}
函数GetRequired(最大值)
{
//这是莫代尔和哑巴,让我哭泣
var numReq='';
if(/ipad | android | webkit/i.test(navigator.userAgent)){
$.showModalDialog({
url:“get_required.php?maxSelect=“+max+”&forceReload=“+Math.random()”,
对话框参数:窗口,
身高:270,
宽度:510,
位置:'顶部',
onClose:function(){numReq=this.returnValue;return numReq;}
});
}否则{
//这很好,因为它会弹出一个新窗口。
numReq=window.showModalDialog(“get_required.php?maxSelect=“+max+”&forceReload=“+Math.random(),window”,对话框高度:250px;对话框宽度:500px;中心:是;帮助:否;可调整大小:否;滚动:否;状态:否;边缘:凸起”);
返回numReq;
}
}
函数()的作用{
//各种UI值重置
printText();
}
有问题的主代码(我的回调版本):


如果(喵喵喵喵)
{
....
}
其他的
{
var requiredCount=GetRequired(arrConfigSet[--lenS][2]);
//所以,这是我的问题,上面的这一行在Firefox/非webkit上运行良好,因为
//弹出窗口使事情运行良好和伟大的。有了这个模式的东西,我们发现
//在internet上(在getRequired()中),这行代码继续运行并运行下面的内容
//线路一直在走,我需要这个
//停在这里。我试着做一些类似于将requiredCount放入一个while循环的事情,但是
//然后它说UncaughtTypeError:无法读取该变量requiredCount上未定义的属性“2”。
//对于记录而言,如果按钮上的onClick事件产生了影响,则所有这些都是通过该事件完成的。
//
//
//警报('test:'+arrConfigSet[-lenS][2]);//返回一个值!
//所需var计数;
//while(变量类型_此处==='undefined'){
//requiredCount=GetRequired(arrConfigSet[--lenS][2]);
//      }
//当在Webkit中时,它会一直运行,我需要设置并最终确定requiredCount
//模式窗口在继续之前消失。
arrConfigSet[lenS][1]=所需计数;
arrConfigSet[lenS][2]++;
}
arrConfigItem[lenI]=新数组();
...
...
回调();
}
函数GetRequired(最大值)
{
//这是莫代尔和哑巴,让我哭泣
var numReq='';
if(/ipad | android | webkit/i.test(navigator.userAgent)){
$.showModalDialog({
url:“get_required.php?maxSelect=“+max+”&forceReload=“+Math.random()”,
对话框参数:窗口,
身高:270,
宽度:510,
位置:'顶部',
onClose:function(){numReq=this.returnValue;return numReq;}
});
}否则{
//这很好,因为它会弹出一个新窗口。
numReq=window.showModalDialog(“get_required.php?maxSelect=“+max+”&forceReload=“+Math.random(),window”,对话框高度:250px;对话框宽度:500px;中心:是;帮助:否;可调整大小:否;滚动:否;状态:否;边缘:凸起”);
返回numReq;
}
}
函数()的作用{
//各种UI值重置
printText();
}
有人知道如何让JS停止运行直到脚本得到返回值吗?它很好地完成了任务,但是当它完成任务时,剩下的代码将在运行之后完成


注意,我们运行的是jQuery1.4,所以promise()是不可能的,我也没有能力去推动任何更新的东西。代码有点像老鼠窝一样。

您可以实现自己的简单延迟库,或者只需传入一个函数,当一切都完成时,get将调用该函数。简单的例子:

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。

您不能暂停Javascript执行以等待异步结果。这根本做不到。相反,您必须重新构造代码,将需要等待结果的代码放入完成回调(或从回调调用)中。基本上:只需将代码分成两半,包装成函数before()和after(),使任何共享变量全局/psudeo全局,然后从before()的回调调用after()(chrome)或从before()的底部(其他所有人)
<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(); 
}
var whenDone = function(res) { alert('I\'m Done: ' + res); };

doComplexTask(whenDone);
function doComplexTask(completeFn) {
    //Do some crazy work here that takes a long time (Or is asynchronous)
    var res = crazyWork();
    completeFn(res);
}