How do i use setTimeout for a chat system

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #16
    what JavaScript lib do you use? jQuery? ... what errors do occur?

    Comment

    • sarah aziz
      New Member
      • Sep 2007
      • 31

      #17
      jquery
      No errors occur at all just not working

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #18
        ... if it's not working then somwhere has to be an error. so lets start to trace that down together. you start with:

        Code:
        $(document).ready(function() {
            timestamp = 0;
            
            updateMsg();
            
            $("form#chatform").submit(function() {
                $.post("db.php", {
                    message: $("#msg").val(),
                    name: $("#name").val(),
                    action: "postmsg",
                    time: timestamp
                }, function(data) {
                    $("#msg").empty();
                    addMessages(data);
                });
                return false;
            });
        });
        the first request is done by updateMsg() and the second is instantly done after this by submitting some form-data. so in both success-handlers you might trace the response ... what do you get back there?

        Comment

        • sarah aziz
          New Member
          • Sep 2007
          • 31

          #19
          Hey gits
          I changed again to return an xml response now i want to know where to place the setTimeout
          Code:
          	function addMessages()
          	{
          			//alert($("#msg").val());
          		$.ajax({
          			url:"db.php",
          			type:"post",
          			data:"message="+$("#msg").val(),
          			datatype:"xml",
          			success:function(data)
          			{	$("#t1").prepend('<tr><td>bullet</td><td>reply</td><td>'+$("time",data).text()+'</td><td>De</td><td>A</td><td>'+$("message",data).text()+'</td><td>Gr</td></tr>');
          			}
          		  });
          	}
          
          function updateMsg()
          {
          	$.ajax({
          		url:"db.php",
          		type:"post",
          		datatype:"xml",
          		success:function(data)
          		{
          			addMessages();
          		}
          	});
          }
          Thanks
          Last edited by gits; Aug 5 '09, 03:13 PM. Reason: added code tags

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #20
            inside a success-handler and just call the method that should be called again.

            Comment

            • sarah aziz
              New Member
              • Sep 2007
              • 31

              #21
              Hi Gits
              HELP ME
              I am going crazy
              take a look at the code and tell me what do you think
              Can you test it please because if it doesnt work i will kill myself
              Code:
              $(document).ready(function()
              {
              	updateMessages();
              
              })
              	function addMessages()
              	{
              			$.ajax({
              				url:"db.php",
              				type:"post",
              				data:"message="+$("#msg").val()+"&action="+"post",
              				success:function()
              				{	
              				 	updateMessages();
              				}
              
              			  });
              		}
              	function updateMessages()
              	{
              			$.ajax({
              				url:"db.php",
              				type:"post",
              				data:"action="+"update",
              				datatype:"xml",
              				success:function(data)
              				{		$("#t1").prepend('<tr><td>bullet</td><td>reply</td><td>'+$("time",data).text()+'</td><td>De</td><td>A</td><td>'+$("message",data).text()+'</td><td>Gr</td></tr>');
              								
              				}
              			
              			  });
              	setTimeout("updateMessages",7000);			
              		}
              Last edited by gits; Aug 7 '09, 11:19 PM. Reason: added code tags

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #22
                wait a moment. let's first check what you have. is the code from post #19 working without a timeout ... so i mean ... do you get the result on the page the first time when it runs?

                Comment

                • sarah aziz
                  New Member
                  • Sep 2007
                  • 31

                  #23
                  It runs onone page not the other
                  I want when i send the message it appears on both pages

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #24
                    ok i'm simply confused now. lets have a look at your latest code (i guess you use the code from post #21), since that is the latest thing that is to follow at the moment. when the doc is ready you call updateMessages( ) which itself should call updateMessages( ) after 7secs again but you have a syntax error here:

                    Code:
                    setTimeout("updateMessages", 7000);
                    must be:

                    Code:
                    setTimeout(updateMessages, 7000);
                    the better way would be to place it in the success-handler but the current calls should work then. your function addMessages() is useless now since it is never called?

                    and what do you mean with 2 pages? this is the first time you mention that it isn't just one?

                    Comment

                    • sarah aziz
                      New Member
                      • Sep 2007
                      • 31

                      #25
                      Hey Gits
                      I want to achieve chatting between 2 clients so this is why i am using setTimeout
                      I want when i write a message on one page it appears on the 2 pages
                      If you have other questions this is my email [edit]removed[edit]
                      bacause i want to solve this problem

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #26
                        please don't post email-addresses in the technical forums, or do you want to get spammed? :) ... and at least it is against the posting rules.

                        to the problem: i think i don't get it quite well, let me summarize:

                        - you have written a webclient for a webchat-application
                        - you open it and log in as User A
                        - you are able to post msg and see the messages at this page?

                        -> that is the code that i saw until now ...

                        - now you open another page and open the client again
                        - you login with another user B

                        - here is the problem? what is not working? i don't understand what the timeout should have to do with that ... since both of the windows should use the same code?

                        Comment

                        • sarah aziz
                          New Member
                          • Sep 2007
                          • 31

                          #27
                          Hi Gits
                          sorry i must have forgotten the rules
                          yes this is what i do

                          yes when i login with another user there is lag in time between messages

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5388

                            #28
                            so the problem is the lag in time? ...

                            Comment

                            • sarah aziz
                              New Member
                              • Sep 2007
                              • 31

                              #29
                              Hey Gits the problem is that the message don't appear on same pages at the same time
                              I want it to appear as if both clients are chatting when i send from one page last messages appear on both pages
                              This is the problem

                              Comment

                              • gits
                                Recognized Expert Moderator Expert
                                • May 2007
                                • 5388

                                #30
                                this will not work ... you will always have to poll the messages from the client ... and in case you don't want to poll every second then you will have that delay ... that is the design of web-technology ...

                                Comment

                                Working...