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

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

问题描述

我想使用 http://abeautifulsite.net/notebook/87上的jQuery提醒对话方块库而不是默认警报(在我看来相当可怕)。这似乎是一个伟大的图书馆,但没有一个例子如何使用jConfirm库。



我需要这样做:

  function confirm(){
var result = false;
var response = false;
jConfirm('你确定吗?','确认对话框',
函数(r){
result = r;
response = true;
return r;
});
if(response == true){
alert(result);
return result;
}
else {
//等待响应
alert('hi');
}
}

$ b



我在插件的网站(今天早上)发表了一个评论,谷歌搜索javascript并等待回调完成,没有结果。 / p>

在javascript执行之前,如何正确使用回调以获得结果的任何想法?



感谢。

解决方案

  jConfirm('你确定吗?','Confirmation Dialog' ,
function(r){
result = r;
response = true;
return r;
}
);
if(response == true){

这掩盖了对事件顺序的误解使用异步代码发生。


  1. jConfirm被调用,接收

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

  3. == true)'行执行。真的这应该只读if(response),布尔比较是多余的。但无论如何,回应当然是错误的。

  4. 用户点击jConfirm的用户界面。

  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天全站免登陆