Javascript 同步jquery调用-如何等待jquery方法执行?

Javascript 同步jquery调用-如何等待jquery方法执行?,javascript,jquery,dom,custom-component,Javascript,Jquery,Dom,Custom Component,我是jQuery新手。我正在使用自定义消息框。我用过 现在当我试着像这样使用它的时候 function myFunction(){ $.msgBox({ title:"Custom Alert", content:"Hello World!" }); /* some code goes here */ // For example alert("executing after Custom Alert.."); } 这里两个都

我是jQuery新手。我正在使用自定义消息框。我用过

现在当我试着像这样使用它的时候

function myFunction(){

    $.msgBox({
       title:"Custom Alert",
       content:"Hello World!"
    });

   /* some code goes here */
   // For example 
   alert("executing after Custom Alert..");
}
这里两个都是异步调用的,两个弹出窗口都显示了

现在我想先执行jQuery的第一个块,然后显示警报框

在我读到的某个地方,脚本是异步的,所以是否有任何同步调用的解决方案


是,这可以通过使用success/callback函数来完成。。 但是我想做一些事情,比如我们的基本“confirm()”方法

所以它应该像

function myConfirm(message){

 $.msgBox({
     title: "Confirmation !!",
     content: message,
     type: "confirm",
     buttons: [{ value: "Yes" }, { value: "No" }],
     success: function (result) {
        if (result == "Yes") {
            return true;  // kindly...i dont know this is proper way to return value.. 
        }else{
            return false; // kindly...i dont know this is proper way to return value.. 
        }
     }
  });
}
现在当我把它叫做。。我想要这样的

var r = myConfirm("What do u like to choose?");

/* some operation will do on 'r' */
/* also to continue to next operation*/
之后,我将对返回值执行下一个操作。
这可以使我们的自定义myConfirm()框方法像基本的confirm()方法一样工作。

尝试下面的方法,在success函数内发出警报并检查

$.msgBox({
    title:"Custom Alert",
    content:"Hello World!",
    success: function () {
         alert("executing after Custom Alert..!");
    }
});

您必须使用回调函数。你看到成功场了吗?这是来自jquery msgBox网站

$.msgBox({
    title: "Are You Sure",
    content: "Would you like a cup of coffee?",
    type: "confirm",
    buttons: [{ value: "Yes" }, { value: "No" }, { value: "Cancel"}],
    success: function (result) {
        if (result == "Yes") {
            alert("One cup of coffee coming right up!");
        }
    }
});

谢谢Saranya!!这是一种方式。。。但请检查我的编辑。我希望它像基本的confirm()方法一样..给出警告消息,而不是返回true和false。谢谢lombausch!!请检查我的编辑。我希望它像基本的confirm()方法一样。。
$.msgBox({
    title: "Are You Sure",
    content: "Would you like a cup of coffee?",
    type: "confirm",
    buttons: [{ value: "Yes" }, { value: "No" }, { value: "Cancel"}],
    success: function (result) {
        if (result == "Yes") {
            alert("One cup of coffee coming right up!");
        }
    }
});