views:

55

answers:

1
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:

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:

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?

Thanks, Hristo

+7  A: 

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(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?

Pekka
+1 Beat me by two seconds.
MvanGeest
You are correct.
SLaks
+1 was just about to say that
TheLQ
javascript closures = win...you beat me by a minute :-(
Bob Fincheimer
@SLaks cheers, good to know!
Pekka
Yes... both 'to' and 'from' both get passed into the function correctly. This seemed to get rid of the error however it caused an infinite loop and the message I post keeps getting posted infinitely.
Hristo
@Hristo I think that's because your function is not specifying a condition when *not* to add the `setTimeout()`?
Pekka
Could it be that I'm POST-ing and then calling the same function which would create the loop?
Hristo
@Hristo yes. What you have right now is designed to work as an infinite loop.
Pekka
Ok. Thanks. I'll try to do it with GET.
Hristo
@Hristo no problem, but that won't change the infinite loop, will it? You are aware that the second parameter to setTimeout is in *milliseconds*, not seconds? Maybe you mean `1000` instead of `1`?
Pekka
Yea infinite loop still exists. Yes I am aware that it is in milliseconds. No matter though, if I do 1000 it loop infinitely again but just slower. How could I just GET only once for each update?
Hristo
@Hristo frequent polling is the way to do it... If you want to react to updates only, you are looking for a JS technique called COMET. http://stackoverflow.com/search?q=js+comet
Pekka
My goal here is when I enter a message, it will be stored in my database then displayed on the chat window. Right now I'm storing in the DB and then retrieving it so it is displayed. So I only want to get it once from the database. I guess the 'entering' of a message should trigger a set of events that write to DB, get from DB, display. I'll look into the COMET idea. Thanks!
Hristo