使用模态库从嵌套异步调用中创建javascript同步代码?

时间:2015-03-27 04:26:25

标签: javascript jquery asynchronous synchronous

所以,这可能有点令人困惑。不久之后当Chromium决定杀死他们的模态支持时,我们的地方找到了一个似乎工作得很好的库(是的,他们希望它在webkit / non-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或要刷新的东西的值,所以时间不是问题。很棒,很棒。

我现在处于这种情况,这种模态调用正处于一堆其他计算的中间。如果它到达这个ifthens的链条,它会发射。但是,上面的代码似乎使它成为一个问题。我已经尝试过像下面这样的回调(也许我只是写错了)并且没有用。地狱,上面的URL中的评论说这样做有点棘手。这有点(我希望)就足够了。 所以,我想点击AddToConfig(),ClearSelections(),printText()这看起来很简单,但是GetRequired asynch-y调用会把所有东西都抛弃。

有问题的主要代码(原件):

//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停下来有任何想法?它很好,但到它的时候,其余的代码运行后。

注意,我们正在运行jQuery 1.4,所以promise()是不可能的,我无法推动任何更新的东西。代码有点像老鼠的窝。

1 个答案:

答案 0 :(得分:0)

您可以实现自己的简单延迟库,或者只是传递一个在完成所有内容时调用的函数。简单的例子:

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 here