同步 jquery 调用 - 如何等到 jquery 方法执行竞争

Syncronous jquery call - How to wait until jquery method executed compete?

本文关键字:jquery 方法 竞争 执行 调用 同步      更新时间:2023-09-26

我是jQuery的新手,我正在使用自定义消息框。我用过jQuery.msgBox()。

现在,当我尝试像这样使用它时

function myFunction(){
    $.msgBox({
       title:"Custom Alert",
       content:"Hello World!"
    });
   /* some code goes here */
   // For example 
   alert("executing after Custom Alert..");
}

这里两者都是异步调用的,两个弹出窗口都得到了显示,

现在我希望第一个jQuery块首先执行,然后应该显示警报框。

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


是的,这可以使用成功/回调函数来完成。但我想做的事情就像我们基本的"确认()"方法

var r=confirm("Press a button!")
if (r==true)
  {
  alert("You pressed OK!")
  }
else
  {
  alert("You pressed Cancel!")
  }

所以它应该像...

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() 方法一样工作。

尝试以下操作,在成功功能中发出警报并检查。

$.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!");
        }
    }
});