javascript错误:未定义变量。

时间:2022-06-11 18:51:07
to is not defined
[Break on this error] setTimeout('updateChat(from, to)', 1); 

I'm getting this error... I'm using Firebug to test and this comes up in the Console. The error corresponds to line 71 of chat.js and the whole function that wraps this line is:

我得到这个错误…我使用Firebug测试,这在控制台上出现。该错误对应于聊天的第71行。js和包装这条线的整个函数是:

function updateChat(from, to) {

    $.ajax({
        type: "POST",
        url: "process.php",
        data: {
            'function': 'getFromDB',
            'from': from,
            'to': to
        },
        dataType: "json",
        cache: false,
        success: function(data) {

            if (data.text != null) {
                for (var i = 0; i < data.text.length; i++) {  
                    $('#chat-box').append($("<p>"+ data.text[i] +"</p>"));
                }
                document.getElementById('chat-box').scrollTop = document.getElementById('chat-box').scrollHeight;
            }
            instanse = false;
            state = data.state;
            setTimeout('updateChat(from, to)', 1); // gives error
        },  
    });
}

This links to process.php with function call getFromDB and the code for that is:

这个链接的过程。php函数调用getFromDB,代码为:

case ('getFromDB'):

    // get the sender and receiver user IDs from their user names
    $from = mysql_real_escape_string($_POST['from']);
    $query  = "SELECT `user_id` FROM `Users` WHERE `user_name` = '$from' LIMIT 1";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    $fromID = $row['user_id'];  

    $to = mysql_real_escape_string($_POST['to']);
    $query  = "SELECT `user_id` FROM `Users` WHERE `user_name` = '$to' LIMIT 1";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    $toID = $row['user_id'];

    $query = "SELECT * FROM `Messages` WHERE `from_id` = '$fromID' AND `to_id` = '$toID' LIMIT 1";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)) {

        $text[] = $line = $row['message'];
        $log['text'] = $text;

    }

    break;

So I'm confused with the line that is giving the error. setTimeout('updateChat(from,to)',1); aren't the parameters to updateChat the same parameters that came into the function? Or are they being pulled in from somewhere else and I have to define to and from else where? Any ideas how to fix this error?

所以我和给出误差的直线混淆了。setTimeout(“updateChat(,),1);updateChat的参数不是函数的参数吗?或者他们是从别的地方被拉进来的我必须定义,从其他地方?有什么办法解决这个错误吗?

Thanks, Hristo

谢谢,斯托伊

1 个解决方案

#1


7  

This could be because when defining the setTimeout function this way, the current function's scope doesn't apply. I don't know exactly to be honest. Should be easy to find out, though: Try

这可能是因为在定义setTimeout函数时,当前函数的作用域并不适用。我不知道确切地说什么。不过,应该很容易找到答案:试试?

 setTimeout(function() { updateChat(from, to) }, 1);

If it works, that's it.

如果可行,就是这样。

if that's not it: Are you sure to gets passed to your first updateChat() call in the first place?

如果不是这样,那么您肯定会第一次被传递到您的第一个updateChat()调用吗?

#1


7  

This could be because when defining the setTimeout function this way, the current function's scope doesn't apply. I don't know exactly to be honest. Should be easy to find out, though: Try

这可能是因为在定义setTimeout函数时,当前函数的作用域并不适用。我不知道确切地说什么。不过,应该很容易找到答案:试试?

 setTimeout(function() { updateChat(from, to) }, 1);

If it works, that's it.

如果可行,就是这样。

if that's not it: Are you sure to gets passed to your first updateChat() call in the first place?

如果不是这样,那么您肯定会第一次被传递到您的第一个updateChat()调用吗?