Jump to content

Recursive Loop In My Chat App


chibineku

Recommended Posts

I thought I had figured out every aspect of this little chat app I was working on yesterday, but I am having a problem. The app starts by checking the value of the variable `check`. This holds the echoed value of $_SESSION["poster"]. If this isn't set, then the user is prompted for a username. The return value is sent to a script which again checks for $_SESSION["poster"]. If it isn't set, then it sets it with the prompted value. The function then reloads the page. This time, $_SESSION["poster"] should be set (I discovered that using AJAX has that one downside that setting things to session). Anyway, when you open the page in Firefox, it continually prompts for username and only breaks if you cancel the prompt. Well, it did, now it doesn't even break if you do that! I tried instead setting a cookie which seems to work again only with a page refresh, which if done as below, causes problems.Here is the javascript (jQuery) code:

$(document).ready(function() {var logged_in = '<?php $logged_in = date("Y-m-d G:i:s", time()); echo $logged_in; ?>';var check = '<?php echo $_COOKIE["poster"];?>';if(check == '') {var poster = prompt("Please enter a username");$.post("do_chat.php5", {'poster':poster}, function(data){});window.location.href = window.location.href;}setTimeout(reload, 400);$('#send').click(function() {fetch_messages();});$('#message').bind("keypress", function(e) {  if(e.which == 13 || e.keycode == 13) {fetch_messages();  }}); $('#message').focus();});function reload() {logged_in = '<?php echo $logged_in; ?>';   var poster = '<?php echo $_COOKIE["poster"];?>';$.post('do_chat.php5', {'message':'','logged_in':logged_in,'poster':poster}, function(data) {	$('#chatWindow').html(data);	});	$.post('users.php5', {}, function(data) { $('#users').html(data);});	setTimeout(reload, 2000);}function fetch_messages() { if($('#message').val()!='') {  var text = $('#message').val();  var poster = '<?php echo $_COOKIE["poster"];?>'; $.post('do_chat.php5', {'message':text,'poster':poster,'logged_in':logged_in}, function(data) { $('#chatWindow').html(data); });}$('#message').val('');$.post('users.php5', {}, function(data) { $('#users').html(data);});}

Link to comment
Share on other sites

That recursion is intentional - I should have said that. There isn't anything in that function that re-prompts for username, it just fetches the most recent messages and displays them in the chat window

Link to comment
Share on other sites

You don't really need to clear a timeout, unless you want to cancel it. Since a timeout only runs once clearing it after it runs doesn't do anything.You need to move the code to reload the page inside the callback function for the ajax request. Ajax is asynchronous, which means that Javascript sends the request out and doesn't wait for it to come back, it just executes the next line of code immediately. So you're sending the request out and then immediately reloading without waiting for the request to finish. Move the reload code into the ajax callback so that it reloads the page once the request finishes.

Link to comment
Share on other sites

Ah, of course. You can see from the fact that the first call to the reload function only fires after 400ms that I had considered the length of time the ajax call takes at one point, but just not this one. Works now, ty :)

Link to comment
Share on other sites

Yeah! i get that, again thanks again for stating the obvious, give yourself another medal.i'm going to claim my medal now!unless a settimeout is within a function that is set to run the same function again at a set period, then it will loop again, again, again........medal please.

Link to comment
Share on other sites

If anyone wants to use the room, it'll be open occasionally at crappy chatty

Link to comment
Share on other sites

j00 are on fire, hombre! :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...