Javascript - 等待一个函数完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10311617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:25:12  来源:igfitidea点击:

Javascript - Wait a function to finish

javascript

提问by Thai Tran

I need to implement a confirm box replacement by using jquery dialog. I have a calling function like this

我需要使用 jquery 对话框来实现确认框替换。我有一个这样的调用函数

function callingFunc() {

 var a = confirmJquery("text", 300, 300, "ok", "cancel"); 

 if (a == true) {
   .....
 }
 else {
   .... 
 }
}

This is the confirmJquery function

这是confirmJquery函数

function confirmJquery(msg, width, height, txtOk, txtCancel) {

    var div = document.createElement('div');
    div.className = "confirmJquery";

    var span = document.createElement('span');
    $(span).html(msg);
    div.appendChild(span);

    var buttonOk = document.createElement('button');
    buttonOk.className = 'buttonStyleBigger';
    $(buttonOk).html(txtOk);

    var buttonCancel = document.createElement('button');
    buttonCancel.className = 'buttonStyleBigger';
    $(buttonCancel).html(txtCancel);

    var divBottom = document.createElement('div');
    divBottom.className = 'dialogAction';

    divBottom.appendChild(buttonOk);
    divBottom.appendChild(buttonCancel);

    div.appendChild(divBottom);

    var dialog = window.parent.$(div).appendTo(window.parent.document.body);

     // open the dialog
    dialog.dialog({
        height: height,
        width: width,
        resizable: false,
        // add a close listener to prevent adding multiple divs to the document
        close: function(event, ui) {
            // remove div with all data and events
            dialog.remove();
        },
        modal: true
    });

    $(buttonOk).bind('click', function(){
        return true;

    });

    $(buttonCancel).bind('click', function() {

        return false;
    });

}

The problem is, the confirmJquery function always finish before the button (Ok or Cancel) is pressed; hence, there is no value in the calling function. I need to make the confirmJquery waits until user press the button and then function finish and the rest of the calling function continues. How can i do that ?

问题是,confirmJquery 函数总是在按下按钮(确定或取消)之前完成;因此,调用函数中没有值。我需要让confirmJquery 等待,直到用户按下按钮,然后函数完成,其余的调用函数继续。我怎样才能做到这一点 ?

I need to update more details: I already tried the call back function way. It works perfectly. But, life is not easy like that. This is a very big, old, messy system. Doing that requires me to re-write lot lot of functions, so i need to create a function that act exactly like the confirm function of javascript

我需要更新更多细节:我已经尝试过回调函数的方式。它完美地工作。但是,生活并没有那么容易。这是一个非常庞大、陈旧、凌乱的系统。这样做需要我重新编写很多函数,所以我需要创建一个与 javascript 的确认函数完全一样的函数

回答by Alexander Pavlov

Since your function is going to be asynchronous, you need to use a callback. Something like this:

由于您的函数将是异步的,因此您需要使用回调。像这样的东西:

function myCallback(result)
{
  if (result) {
    // OK
  } else {
    // Cancel
  }
}

function confirmJquery(msg, width, height, txtOk, txtCancel, callback) {
...
    $(buttonOk).bind('click', function(){
        callback(true);
    });

    $(buttonCancel).bind('click', function() {
        callback(false);
    });
}

and

confirmJquery(msg, width, height, txtOk, txtCancel, myCallback);

回答by Christian Romeni

Move the rest of the function inside another function, and execute that second function at the end of the confirmJquery function.

将函数的其余部分移到另一个函数中,并在 confirmJquery 函数的末尾执行第二个函数。

function firstfunction(){

  // Do Some Stuff

  secondfunction();

}

回答by Joseph

first, to avoid a long list of arguments on the receiving side, you can use an object of arguments instead. then send over a callback to confirmJquery

首先,为了避免接收方出现一长串参数,您可以使用参数对象来代替。然后通过回调发送到confirmJquery

function callingFunc() {

    var a = confirmJquery({
        msg:"text", 
        width:300, 
        height:300, 
        txtOk:"ok", 
        txtCancel:"cancel"
    },function(ok){
        if(ok){
            ...
        } else {
            ...
        }
    });
}

function confirmJquery(options,callback) {
    //options.msg
    //options.width
    ...

    $(buttonOk).bind('click', function(){
        callback(true);
    });

    $(buttonCancel).bind('click', function() {
        callback(false);
    });

}

回答by syedi

yes, alexander is right, just reorganize code, one for dialog, one for function based on a flag/msg. just like mvc pattern.

是的,alexander 是对的,只需重新组织代码,一个用于对话框,一个用于基于标志/味精的函数。就像 mvc 模式一样。