Jump to content

Help with refreshing something every 1 second


TeddyKiller

Recommended Posts

I have a simple chat script that runs a script and loads it into a div every 1 second, it should be similar to do it for another div, instead of loading a chat though, it's loading logged in users. (I dont need the page.php to do everything, thats alright, can do that myself) Just need help with the javascript.

 

Here is the Javascript.

 

        <script language="JavaScript" type="text/javascript">
            var sendReq = getXmlHttpRequestObject();
            var receiveReq = getXmlHttpRequestObject();
            var lastMessage = 0;
            var mTimer;
            //Function for initializating the page.
            function startChat() {
                //Set the focus to the Message Box.
                document.getElementById('txt_message').focus();
                //Start Recieving Messages.
                getChatText();
            }        
            //Gets the browser specific XmlHttpRequest Object
            function getXmlHttpRequestObject() {
                if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
                } else if(window.ActiveXObject) {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } else {
                    document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
                }
            }
            
            //Gets the current messages from the server
            function getChatText() {
                if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
                    receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true);
                    receiveReq.onreadystatechange = handleReceiveChat; 
                    receiveReq.send(null);
                }            
            }
            //Add a message to the chat server.
            function sendChatText() {
                if(document.getElementById('txt_message').value == '') {
                    alert("You have not entered a message");
                    return;
                }
                if (sendReq.readyState == 4 || sendReq.readyState == 0) {
                    sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
                    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    sendReq.onreadystatechange = handleSendChat; 
                    var param = 'message=' + document.getElementById('txt_message').value;
                    param += '&name=<?php echo "<span style=\"color:#e11919; font-weight:bold;\">".ucwords($user->username)."</span>";?>';
                    param += '&chat=1';
                    sendReq.send(param);
                    document.getElementById('txt_message').value = '';
                }                            
            }
            //When our message has been sent, update our page.
            function handleSendChat() {
                //Clear out the existing timer so we don't have 
                //multiple timer instances running.
                clearInterval(mTimer);
                getChatText();
            }
            //Function for handling the return of chat text
            function handleReceiveChat() {
                if (receiveReq.readyState == 4) {
                    var chat_div = document.getElementById('div_chat');
                    var xmldoc = receiveReq.responseXML;
                    var message_nodes = xmldoc.getElementsByTagName("message"); 
                    var n_messages = message_nodes.length
                    for (i = 0; i < n_messages; i++) {
                        var user_node = message_nodes.getElementsByTagName("user");
                        var text_node = message_nodes.getElementsByTagName("text");
                        var time_node = message_nodes.getElementsByTagName("time");
                        chat_div.innerHTML += user_node[0].firstChild.nodeValue + ' ';
                        chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font><br />';
                        chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
                        chat_div.scrollTop = chat_div.scrollHeight;
                        lastMessage = (message_nodes.getAttribute('id'));
                    }
                    mTimer = setTimeout('getChatText();',1000); //Refresh our chat in 1 seconds
                }
            }
            //This functions handles when the user presses enter.  Instead of submitting the form, we
            //send a new message to the server and return false.
            function blockSubmit() {
                sendChatText();
                return false;
            }
            //This cleans out the database so we can start a new chat session.
            function resetChat() {
                if (sendReq.readyState == 4 || sendReq.readyState == 0) {
                    sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
                    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    sendReq.onreadystatechange = handleResetChat; 
                    var param = 'action=reset';
                    sendReq.send(param);
                    document.getElementById('txt_message').value = '';
                }                            
            }
            //This function handles the response after the page has been refreshed.
            function handleResetChat() {
                document.getElementById('div_chat').innerHTML = '';
                getChatText();
            }    
        </script>

 

The DIV it should load the page into, just put the name as div_divname or something.

This is the biggest function though.

            function handleReceiveChat() {
                if (receiveReq.readyState == 4) {
                    var chat_div = document.getElementById('div_chat');
                    var xmldoc = receiveReq.responseXML;
                    var message_nodes = xmldoc.getElementsByTagName("message"); 
                    var n_messages = message_nodes.length
                    for (i = 0; i < n_messages; i++) {
                        var user_node = message_nodes.getElementsByTagName("user");
                        var text_node = message_nodes.getElementsByTagName("text");
                        var time_node = message_nodes.getElementsByTagName("time");
                        chat_div.innerHTML += user_node[0].firstChild.nodeValue + ' ';
                        chat_div.innerHTML += '<fontclass="chat_time">' + time_node[0].firstChild.nodeValue +'</font><br />';
                        chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
                        chat_div.scrollTop = chat_div.scrollHeight;
                        lastMessage = (message_nodes.getAttribute('id'));
                    }
                    mTimer = setTimeout('getChatText();',1000); //Refresh our chat in 1 seconds
                }
            }

 

Not sure how to convert that - I dont even know if most of it is needed. There is a scroll bar needed though. No HTML etc.

chat_div is another div, thats for the chat, so you kind of need to duplicate handleRecieveChat and change it slightly.

All help appreciated :)

 

Thanks

 

Edit: Sorry about the wrong code tags D:

 

Link to comment
Share on other sites

I put it like this, however it doesn't work.

      <script language="JavaScript" type="text/javascript">
         var sendReq = getXmlHttpRequestObject();
         var receiveReq = getXmlHttpRequestObject();
         var lastMessage = 0;
         var mTimer;
         var kTimer;
         //Function for initializating the page.
         function startChat() {
            //Set the focus to the Message Box.
            document.getElementById('txt_message').focus();
            //Start Recieving Messages.
            getChatText();
            getChatUsers();
         }      
         //Gets the browser specific XmlHttpRequest Object
         function getXmlHttpRequestObject() {
            if (window.XMLHttpRequest) {
               return new XMLHttpRequest();
            } else if(window.ActiveXObject) {
               return new ActiveXObject("Microsoft.XMLHTTP");
            } else {
               document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
            }
         }
         
         //Gets the current messages from the server
         function getChatText() {
            if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
               receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true);
               receiveReq.onreadystatechange = handleReceiveChat; 
               receiveReq.send(null);
            }         
         }
         function getChatUsers() {
            if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
               receiveReq.open("GET", 'getUsers.php?chat=1', true);
               receiveReq.onreadystatechange = handleReceiveUsers; 
               receiveReq.send(null);
            }         
         }
         //Add a message to the chat server.
         function sendChatText() {
            if(document.getElementById('txt_message').value == '') {
               alert("You have not entered a message");
               return;
            }
            if (sendReq.readyState == 4 || sendReq.readyState == 0) {
               sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
               sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
               sendReq.onreadystatechange = handleSendChat; 
               var param = 'message=' + document.getElementById('txt_message').value;
               param += '&name=<?php echo "<span style=\"color:#e11919; font-weight:bold;\">".ucwords($user->username)."</span>";?>';
               param += '&chat=1';
               sendReq.send(param);
               document.getElementById('txt_message').value = '';
            }                     
         }
         //When our message has been sent, update our page.
         function handleSendChat() {
            //Clear out the existing timer so we don't have 
            //multiple timer instances running.
            clearInterval(mTimer);
            getChatText();
         }
         //Function for handling the return of chat text
         function handleReceiveChat() {
            if (receiveReq.readyState == 4) {
               var chat_div = document.getElementById('div_chat');
               var xmldoc = receiveReq.responseXML;
               var message_nodes = xmldoc.getElementsByTagName("message"); 
               var n_messages = message_nodes.length
               for (i = 0; i < n_messages; i++) {
                  var user_node = message_nodes.getElementsByTagName("user");
                  var text_node = message_nodes.getElementsByTagName("text");
                  var time_node = message_nodes.getElementsByTagName("time");
                  chat_div.innerHTML += user_node[0].firstChild.nodeValue + ' ';
                  chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font><br />';
                  chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
                  chat_div.scrollTop = chat_div.scrollHeight;
                  lastMessage = (message_nodes.getAttribute('id'));
               }
               mTimer = setTimeout('getChatText();',1000); //Refresh our chat in 1 seconds
            }
         }
         //Function for handling the return of chat users
         function handleReceiveUsers() {
            if (receiveReq.readyState == 4) {
               var online_div = document.getElementById('div_online');

               for (i = 0; i < n_messages; i++) {
                  online_div.scrollTop = chat_div.scrollHeight;
               }
               kTimer = setTimeout('getChatUsers();',1000); //Refresh our chat in 1 seconds
            }
         }
         //This functions handles when the user presses enter.  Instead of submitting the form, we
         //send a new message to the server and return false.
         function blockSubmit() {
            sendChatText();
            return false;
         }
         //This cleans out the database so we can start a new chat session.
         function resetChat() {
            if (sendReq.readyState == 4 || sendReq.readyState == 0) {
               sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
               sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
               sendReq.onreadystatechange = handleResetChat; 
               var param = 'action=reset';
               sendReq.send(param);
               document.getElementById('txt_message').value = '';
            }                     
         }
         //This function handles the response after the page has been refreshed.
         function handleResetChat() {
            document.getElementById('div_chat').innerHTML = '';
            getChatText();
         }   
      </script>

 

It doesn't run the page I think.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.