[英]javascript error: variable is not defined
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. 我收到此错误...我正在使用Firebug进行测试,这在控制台中显示。 The error corresponds to line 71 of chat.js and the whole function that wraps this line is:
该错误对应于chat.js的第71行,而包装此行的整个函数是:
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: 这通过函数调用
getFromDB
链接到process.php,其代码为:
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? 不是要
updateChat
的参数与函数中的参数相同吗? 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?
任何想法如何解决此错误?
Thanks, Hristo 谢谢,克里斯托
This could be because when defining the setTimeout
function this way, the current function's scope doesn't apply. 这可能是因为以这种方式定义
setTimeout
函数时,当前函数的范围不适用。 I don't know exactly to be honest. 老实说,我不知道。 Should be easy to find out, though: Try
不过应该很容易找出:尝试
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? 如果不是那样的话:首先确定
to
传递给您的第一个updateChat()
调用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.