如果在开始放置一个alert(),为什么javascript函数会执行?

时间:2022-11-15 19:58:21
var callback = function(result){
             //alert(result);
             var json = eval('('+result+')');
             if(json.criticalerror==true) dialogCriticalError(json.errormessage);
                else{
                    if(json.error==true) dialogError(json.errormessage);
                    else{
                        // Do something else
                    }
                }
         };

When this callback-function is executed the "Do something else" part is called without problems. But in the case the json.error is true the dialogError-function is not executed. I've checked the transmitted JSON in Firebug. Everything is ok. The result is a JSON string as it should be.

当执行此回调函数时,将毫无问题地调用“Do something else”部分。但这里是json。错误为真,没有执行对话框错误函数。我在Firebug中检查了传输的JSON。一切都是好的。结果是一个JSON字符串。

The interesting thing is, that it actually is executed if i call the JSON-response with an alert() function at the beginning of the callback function. I'm new to JavaScript and probably missing something obvious but i just can't figure it out. Where's the bug?

有趣的是,如果我在回调函数的开头使用alert()函数调用JSON-response,那么它实际上会被执行。我是JavaScript新手,可能漏掉了一些明显的东西,但我就是搞不清楚。错误在哪里?


EDIT:

编辑:

It seems the problem is the time. If i put a 100ms delay between the JSON-result and the actual callback, everything works perfectly. But this can't be right... I'm kind of clueless.

看来问题出在时间上。如果我在JSON-result和实际回调之间设置100ms的延迟,那么一切都会很好地工作。但这不可能是对的……我有点笨。

(Oh and by the way: the communication is done by JBoss Seam Remoting)

(哦,顺便说一下:沟通是由JBoss Seam Remoting完成的)

The whole function looks like that:

整个函数是这样的:

        function nextNode() {
         var callback = function(result){
             var json = JSON.parse(result);
             if (json.criticalerror==true) {
                    dialogCriticalError(json.errormessage);
                }else if (json.error==true) {
                    dialogError(json.errormessage);
                }else {
                        document.getElementById('currentTree').innerHTML = json.state;
                        document.getElementById('countTrees').innerHTML = json.amountSteps;
                        document.getElementById('iframe').contentWindow.importGraph(json.tree);
                        document.getElementById('relevantnode').innerHTML = json.node;
                        createNodeBar(json);
                }                
         };
        manager.nextNode(callback);
    }

The manager object is provided by the Seam Framework through the following function:

Seam框架通过以下函数提供manager对象:

var manager = Seam.Component.getInstance("solverTreeStructure");


LAST EDIT:

最后的编辑:

Okay now i got the definite source of the problem. Its not not the Seam Remoting but the dialogError() function and the library it uses to display the dialog.

现在我知道问题的确切来源了。它不是Seam Remoting,而是dialogError()函数和它用来显示对话框的库。

The dialogError() function looks like that:

对话框error()函数如下:

function dialogError(error){
        TINY.box.show({html:error,width:250,height:100,close:true,mask:true,opacity:20,topsplit:3})
    }

It uses a small dialog library called TINYBOX. Now this library offers a variety of parameters to configure the dialog boxes. The 'mask' parameter caused all the trouble. It is resposible for darkening the background of the dialog box. If its turned on, TINYBOX needs a start-delay in order to work with the callback function. (i have NO idea why)

它使用一个名为TINYBOX的小对话框库。现在这个库提供了各种参数来配置对话框。“蒙版”参数引起了所有的麻烦。它负责使对话框的背景变暗。如果它打开了,TINYBOX需要一个start-delay来处理回调函数。(我不知道为什么)

But for those who like riddles:

但对于那些喜欢谜语的人:

Thats the library. Its very small and clear. Unfortunately my JavaScript skills are not yet sophisticated enough to understand it. http://www.scriptiny.com/2011/03/javascript-modal-windows/

这是图书馆。它很小,很清晰。不幸的是,我的JavaScript技能还不够复杂,还不能理解它。http://www.scriptiny.com/2011/03/javascript-modal-windows/

Thats the answer. Have a nice day folks! ;)

这就是答案。祝大家愉快!,)

2 个解决方案

#1


2  

Just a general advice: do not mix blocks with and without {}. The following form is much more readable and you can pinpoint your problem quicker.

简单建议:不要混合使用和不使用{}的块。下面的表单可读性更好,您可以更快地找到问题所在。

console.log(json);
if (json.criticalerror == true) {

    console.log("criticalerror");
    dialogCriticalError(json.errormessage);

} else if (json.error == true) {

    console.log("error");
    dialogError(json.errormessage);

} else {

    console.log("something else");
    // Do something else
}

#2


0  

It seems the problem is the time. If i put a 100ms delay between the JSON-result and the actual callback, everything works perfectly. But this can't be right... I'm kind of clueless.

看来问题出在时间上。如果我在JSON-result和实际回调之间设置100ms的延迟,那么一切都会很好地工作。但这不可能是对的……我有点笨。

Sounds like your doing asynchronous (ajax) communication. WHere is the rest of your code that asks the server for some data. Your handling the result before the server gives it to you. This explains the delay.

听起来像是在进行异步(ajax)通信。请求服务器提供一些数据的其余代码在哪里?在服务器给您结果之前处理结果。这就解释了延迟。

#1


2  

Just a general advice: do not mix blocks with and without {}. The following form is much more readable and you can pinpoint your problem quicker.

简单建议:不要混合使用和不使用{}的块。下面的表单可读性更好,您可以更快地找到问题所在。

console.log(json);
if (json.criticalerror == true) {

    console.log("criticalerror");
    dialogCriticalError(json.errormessage);

} else if (json.error == true) {

    console.log("error");
    dialogError(json.errormessage);

} else {

    console.log("something else");
    // Do something else
}

#2


0  

It seems the problem is the time. If i put a 100ms delay between the JSON-result and the actual callback, everything works perfectly. But this can't be right... I'm kind of clueless.

看来问题出在时间上。如果我在JSON-result和实际回调之间设置100ms的延迟,那么一切都会很好地工作。但这不可能是对的……我有点笨。

Sounds like your doing asynchronous (ajax) communication. WHere is the rest of your code that asks the server for some data. Your handling the result before the server gives it to you. This explains the delay.

听起来像是在进行异步(ajax)通信。请求服务器提供一些数据的其余代码在哪里?在服务器给您结果之前处理结果。这就解释了延迟。