你能等待javascript回调吗? [英] Can you wait for javascript callback?

查看:131
本文介绍了你能等待javascript回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 http://abeautifulsite.net/notebook/87 而不是默认警报(在我看来看起来很可怕)。这似乎是一个很棒的图书馆,但是没有一个如何使用jConfirm库的例子。



我需要这样做:

  function confirm(){
var result = false;
var response = false;
jConfirm('你确定吗?'''确认对话框'
function(r){
result = r;
response = true;
return r;
});
if(response == true){
alert(result);
返回结果;
}
else {
//等待响应
alert('嗨');
}
}

我的.net按钮的电话:



我已经在插件的网站上发布了评论(就在今天早上),Google搜索了javascript并等待回调完成,没有结果。 / p>

在JavaScript的其余部分执行之前,有关如何正确使用回调函数获得结果的任何想法?



解决方案

  jConfirm('你确定吗?','确认对话框' ,
function(r){
result = r;
response = true;
return r;
}
);
if(response == true){

这导致了对事件顺序的误解这是使用异步代码发生的。只是因为你已经写入内联并不意味着它会严格按照从顶到尾执行。


  1. jConfirm被调用,接收一个函数作为其参数之一,它记住。

  2. jConfirm在页面上显示其UI,并立即返回。

  3. 'if(响应== true)'行执行。真的这应该只是读'if(response)',布尔比较是多余的。但无论如何,反应当然是错误的。您的功能放弃并退出,控制回浏览器。

  4. 用户点击jConfirm的UI。

  5. jConfirm现在只需跳转到行动和通话返回你给它的功能,并记得早些时候。

  6. 你的嵌套函数设置响应为true,对于'if(response == true)'条件来说,这样做太晚了。

您已经编写了// wait for response作为替代,但是没有可以写的JavaScript代码,实际上会那。您的功能必须返回才能控制回浏览器,然后浏览器才能触发jConfirm UI上的点击事件,使处理进行。



使异步代码工作的方式存在同步上下文(反之亦然) - 特别是线程和协同程序(及其有限关系生成器)。但是JavaScript没有这些功能,所以您必须编写代码以适合您的库正在使用的同步或异步模型。


I'm trying to use the jQuery alerts dialog library from http://abeautifulsite.net/notebook/87 instead of the default alerts (which look pretty awful in my opinion). This seems to be a great library, but there is not an example of how to use the jConfirm library.

I need to do something like this:

function confirm() {
        var result = false;
        var response = false;
        jConfirm('are you sure?', 'Confirmation Dialog',
          function(r) {
            result = r;
            response = true;
            return r;
        });
        if (response == true) {
            alert(result);
            return result;
        }
        else {
            //wait for response
            alert('hi');
        }
    }

and my call from my .net button:

I've posted a comment on the plugin's website (just this morning) and did Google searches for javascript and waiting for a callback to complete with no results.

Any ideas on how to use the callback correctly to get the result, before the rest of the javascript executes?

Thanks.

解决方案

jConfirm('are you sure?', 'Confirmation Dialog',
    function(r) {
        result = r;
        response = true;
        return r;
    }
);
if (response == true) {

This betrays a misunderstanding of the sequence of events that occurs using asynchronous code. Just because you've written it inline doesn't mean it's going to execute strictly top-to-bottom.

  1. jConfirm is called, receiving a function as one of its parameters, which it remembers.
  2. jConfirm displays its UI on the page and returns immediately.
  3. The 'if (response==true)' line executes. Really this should just read 'if (response)', the boolean comparison is superfluous. But in any case response is of course false. Your function gives up and exits, giving control back to the browser.
  4. The user clicks jConfirm's UI.
  5. jConfirm only now jumps into action and calls back the function you gave it and it remembered earlier.
  6. Your nested function sets response true, far too late for the 'if (response==true)' condition to do anything with it.

You have written "//wait for response" as an alternative, but there is no JavaScript code you can write that will actually do that. Your function must return to give control back to the browser, before the browser can fire the click events on the jConfirm UI that make processing proceed.

Ways to make asynchronous code work in a synchronous context (and vice versa) exist - in particular threads and coroutines (and their limited relation generators). But JavaScript has none of these features, so you must write your code to fit the synchronous-or-asynchronous model your library is using.

这篇关于你能等待javascript回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆