简体   繁体   English

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

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

I am new to jQuery.I am using Custom message Box. 我是jQuery的新手。我正在使用自定义消息框。 I have used jQuery.msgBox() . 我使用过jQuery.msgBox()

Now while i am trying to use it like 现在,虽然我想尝试使用它

function myFunction(){

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

   /* some code goes here */
   // For example 
   alert("executing after Custom Alert..");
}

Here both are called asynchronously, both Popups got show, 这两个都是异步调用,两个Popup都显示,

now i want first block of jQuery get executed first, then alert box should show. 现在我想首先执行jQuery的第一块,然后应该显示警告框。

somewhere i read Script is asynchronous, so is there any solution to call synchronously. 在某处我读到脚本是异步的,所以有任何解决方案可以同步调用。


Yes this can be done using success / callback function.. but something i want to do like our basic 'confirm()' Method 是的,这可以使用success / callback函数来完成..但我想做的事情就像我们的基本'confirm()'方法

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

So it should work like... 所以它应该像...一样工作

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

now when i call it like.. i want it like 现在,当我称之为...我想要它

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

/* some operation will do on 'r' */
/* also to continue to next operation*/

after that, on the return value i will do next operation . 之后,在返回值上我会做下一步操作。 can this is possible to make our custom myConfirm() box method work like basic confirm() method. 这可以使我们的自定义myConfirm()框方法像基本的confirm()方法一样工作。

Try the below, give alert inside success function and check. 尝试下面的内容,在成功功能中提供警报并检查。

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

You have to use callback functions. 您必须使用回调函数。 you see success field? 你看到成功领域? This is from jquery msgBox website. 这是来自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!");
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM